Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase snapshot.val()返回[对象]_Javascript_Firebase_Firebase Realtime Database_Firebase Authentication - Fatal编程技术网

Javascript Firebase snapshot.val()返回[对象]

Javascript Firebase snapshot.val()返回[对象],javascript,firebase,firebase-realtime-database,firebase-authentication,Javascript,Firebase,Firebase Realtime Database,Firebase Authentication,我正试图从firebase DB中检索用户,但当我打印snapshot.val()时,它会显示[object]。如何打印真实数据?谢谢 FriendlyChat.prototype.GetRandomInterlocator=async function(){ var numberOfUsers=等待此消息。getNumberOfUsersRef(); var randomIndex=Math.floor(Math.random()*numberOfUsers); if(randomIndex

我正试图从firebase DB中检索用户,但当我打印snapshot.val()时,它会显示[object]。如何打印真实数据?谢谢

FriendlyChat.prototype.GetRandomInterlocator=async function(){
var numberOfUsers=等待此消息。getNumberOfUsersRef();
var randomIndex=Math.floor(Math.random()*numberOfUsers);
if(randomIndex==0){
随机指数=1;
}
var ref=await firebase.database().ref('companys/'+this.company+'/users/');
参考limitToFirst(随机索引)。一次('value')。然后(快照=>
{
var user=snapshot.val();
log('GetRandomInterlocator='+用户);
返回用户;
});
}
FriendlyChat.prototype.onAuthStateChanged=函数(用户){
log('entro a onAuthStateChanged');
this.interlector=this.getrandomInterlector();
console.log(this.interlector);

}
只需打印
console.log(用户)而不是包含obj的字符串。
这将复制它:

var obj = [{a: 1}, {a: 'lucas', b: 2}];
console.log('current obj', obj);
然而,由于用户是一个数组,所以您可以这样做,以获得所需的每个对象。(ES6)


只需console.log(user)就可以找到用户对象的结构。谢谢,但我很困惑。它打印对象{SEV9iTd8whgf0VyIbqUiVNaTcAL2:Object}。我如何设置变量='SEV9iTd8whgf0VyIbqUiVNaTcAL2'?这是因为firebase得到了奇怪的数组,类似于它的对象而不是数组,因此
SEV…
是下面对象的键,而不是常规数组中的索引。所以您需要编写
user['SEV9iTd8whgf0VyIbqUiVNaTcAL2']
来获取用户。我一开始就知道这很奇怪,但你使用得越多,它就越“清晰”。这篇博客文章是更好地理解firebase阵列的一个很好的资源:
user.forEach((u, key) => {
    // u contains your user data object which is like u.uid and u.email or whatever keys you got.
    console.log(key, 'key here contains the strange "firebase array index key"');
    console.log(u);
});