2017-04-21 1 views
0

Momentan implementiere ich einen Chat. Nachdem der Benutzer eine Chat-Schaltfläche gedrückt hat, navigiert die App durch den Benutzer zur Chat-Komponente. Der Chat-Inhalt wird einfach in Firebase gespeichert und chatId wird benötigt, um festzustellen, welcher Chat dem Benutzer gehört.Navigation nach AsyncStorage.setItem: _this3.navigateTo ist keine Funktion

Da ich nicht weiß, wie props während der Navigation übergeben, entschied ich mich, die CurrentChatId in AsyncStorage zu speichern. Nach der Navigation zur Chat-Komponente wird die CurrentChatId von AsyncStorage abgerufen, sodass ich den Chat-Inhalt der Firebase zuordnen kann.

Allerdings habe ich den Fehler _this3.navigateTo is not a function mit Code unter:

let ref = FirebaseClient.database().ref('/Chat'); 
ref.orderByChild("chatId").equalTo(chatId).once("value", function(snapshot) { 

    chatId = taskId + "_" + user1Id + "_" + user2Id; 
    if (snapshot.val() == null) { 
     ref.push({ 
      chatId: chatId, 
      taskId: taskId, 
      user1Id: user1Id, 
      user2Id: user2Id, 
     }) 
    } 
    try { 
     AsyncStorage.setItem("CurrentChatId", chatId).then(res => { 
      this.navigateTo('chat'); 
     }); 
    } catch (error) { 
     console.log('AsyncStorage error: ' + error.message); 
    } 
} 

Die Funktion navigateTo aus der Demo-Anwendung von NativeBase

import { actions } from 'react-native-navigation-redux-helpers'; 
import { closeDrawer } from './drawer'; 

const { 
    replaceAt, 
    popRoute, 
    pushRoute, 
} = actions; 

export default function navigateTo(route, homeRoute) { 
    return (dispatch, getState) => { 
    const navigation = getState().cardNavigation; 
    const currentRouteKey = navigation.routes[navigation.routes.length - 1].key; 

    dispatch(closeDrawer()); 

    if (currentRouteKey !== homeRoute && route !== homeRoute) { 
     dispatch(replaceAt(currentRouteKey, { key: route, index: 1 }, navigation.key)); 
    } else if (currentRouteKey !== homeRoute && route === homeRoute) { 
     dispatch(popRoute(navigation.key)); 
    } else if (currentRouteKey === homeRoute && route !== homeRoute) { 
     dispatch(pushRoute({ key: route, index: 1 }, navigation.key)); 
    } 
    }; 
} 

Antwort

1

kopiert Sie sollten binden dies auf die Funktion, die enthält die versuchen & fangen. Die beste Vorgehensweise ist es, diese binden die Konstruktor der Komponente hinzuzufügen:

constructor(props) { 
    super(props); 
    this.myFunctoin = this.myfuction.bind(this); 
} 
+0

Ich habe versucht, die Funktion zu binden, aber es hilft nicht. Ich habe jetzt mehr Code gezeigt. Bezieht es sich auf das 'function (snapshot) {...}' Ding? – ykn121

0

Schließlich habe ich das Problem gelöst. Es ist wirklich, weil this.navigateTo('chat'); drin ist function(snapshot)

ref.orderByChild("chatId").equalTo(chatId).once("value", function(snapshot) { 

    chatId = taskId + "_" + user1Id + "_" + user2Id; 
    if (snapshot.val() == null) { 
     ref.push({ 
      chatId: chatId, 
      taskId: taskId, 
      user1Id: user1Id, 
      user2Id: user2Id, 
     }) 
    } 
} 
try { 
    AsyncStorage.setItem("CurrentChatId", chatId).then(res => { 
     this.navigateTo('chat'); 
    }); 
} catch (error) { 
    console.log('AsyncStorage error: ' + error.message); 
} 

Nehmen Sie aus der Funktion heraus wird das Problem lösen.