在这里您可以看到名为ChatID的变量。
getChatId() {
const IDloc = firebase.database().ref('/rooms');
const newChat = IDloc.push({
title: 'New chat over again'
});
const ChatID = newChat.key;
try {
AsyncStorage.getItem('email').then((email) => {
const membersList = firebase.database().ref('/members').child(ChatID);
const user1 = email
console.log('user1: ', user1);
const user = firebase.auth().currentUser;
membersList.set({
user1: user1,
user2: user.email
});
}).done();
} catch (error) {
console.log('Error has accurated');
}
}
get ref() {
return firebase.database().ref('/messages').child(this.getChatId.ChatID);
}
我需要做的是将此变量从getChatId()传递给ref()并将此ChatID插入子项中。
按照我在代码中所做的方式进行操作时,我得到的是不确定的而不是ChatID。当我尝试使用state = {}进行此操作时,我只会得到null。
我该如何解决?
答案 0 :(得分:1)
如果您正在使用课程,则可以执行以下操作
ChatID = '' //declare the variable outside
getChatId() {
const IDloc = firebase.database().ref('/rooms');
const newChat = IDloc.push({
title: 'New chat over again'
});
this.ChatID = newChat.key; //set the variable
try {
AsyncStorage.getItem('email').then((email) => {
const membersList = firebase.database().ref('/members').child(ChatID);
const user1 = email
console.log('user1: ', user1);
const user = firebase.auth().currentUser;
membersList.set({
user1: user1,
user2: user.email
});
}).done();
} catch (error) {
console.log('Error has accurated');
}
}
get ref() {
return firebase.database().ref('/messages').child(this.ChatID);
}