0
我试图设置一个变量为snapshot.key值。到目前为止,我的变量返回interlocutor = [object Promise],但我想将其设置为实际值,例如SEV9iTd8whgf0VyIbqUiVNaTcAL2,这是节点的关键。 我该怎么做?snapshot.key字符串变量?
谢谢!
这是我的代码:
this.interlocutor = this.getRandomInterlocutor();
FriendlyChat.prototype.getRandomInterlocutor = async function(){
var numberOfUsers = await this.getNumberOfUsersRef();
var randomIndex = Math.floor(Math.random() * numberOfUsers);
if(randomIndex == 0){
randomIndex = 1;
}
var ref = await firebase.database().ref('companies/' + this.company + '/users/');
ref.orderByChild('order').equalTo(randomIndex).on('value', function(userSnapshot) {
userSnapshot.forEach(function(userSnapshot) {
this.interlocutor = userSnapshot.key;
return val.userSnapshot.key;
});
});
}
大概订阅的 '价值' 时,你应该使用一个承诺语法,如例如[firebase-docs](https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot),因此它可能看起来像'.on('value')。t母鸡(功能(userSnapshot){...' –