在这里您可以看到称为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。当我尝试使用状态= {}进行此操作时,我只是得到null。
我该如何修复?
如果您使用的是类,则可以执行以下
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);
}