Firebase的价值是不确定的,当它不应该 [英] Firebase value is undefined when it is not supposed to be

查看:152
本文介绍了Firebase的价值是不确定的,当它不应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Firebase项目。在测试期间

  return user.val()。name; 

将返回一个

  undefined 

值然而

  console.log(user.val()。name)

将返回存储在.name字段中的实际字符串。这是为什么。同样,即使将

  user.val()赋值给

给一个变量,变量保持不确定。请帮忙弄清楚为什么会发生这种情况。我打印到csv。



以下是我的代码:

  var database = firebase.database(); 
var ref2 = database.ref('information /');

var id;
var name;

ref2.on(value,function(one){

one.forEach(function(two){

if(typeof two .val().Id!=='undefined'){
id = two.val().id;
name = program(id); // name undefined

}
else {
id =;
}

csv = name +,+ id +\\\
;


});

下载(csv);

});
};


函数程序(id){

var database = firebase.database();
var ref = database.ref(users /+ id +/);
$ b ref.on('value',function(user){

if(typeof user.val()。name ==='undefined'){
返回null;
}
else {
console.log(user.val()。name); //这并不显示undefined
return user.val()。name ; //当附加到html元素时显示未定义

}
})
}

注意:在firebase数据库中,名称值不为空。它有一个字符串添加到它。

解决方案

我第二次与弗兰克的原因为什么你的函数 program()不起作用。因为 ref.on('value'... 进行异步调用,所以 program()不会等待完成 ref.on 并存在一个未定义返回值。



你可以做的是使用Promise将Promise中的程序()函数中的语句包装进来,并且在完成异步调用时, resolve reject ,根据结果给出。



这是您的函数与承诺:

 函数程序(id){
返回新的Promise(function(resolve,reject){
尝试{
var database = firebase.database();
var ref = database.ref(users /+ id +/);

ref .on('value',function(user){
if(typeof user.val()。name ==='undefined'){
resolve(null);
} else {
consol e.log(user.val()的名字);
resolve(user.val()。name);

})
} catch(e){
reject(e)
}
});
}

然后,您可以通过以下方法阅读结果:

$ (功能(结果){
console.log(结果)
//做你自己的事情)b
$ b

  catch(function(error){
console.log(error)
})

注意:您在for-each语句中执行此块。如果您使用的是Promises,那么您还需要了解如何在循环中使用Promise。作为参考,请检查 Promise.all()


I am working on a firebase project. During testing the

return user.val().name;

will return an

undefined 

value however the

console.log(user.val().name) 

will return the actual string stored in the .name field. Why is that. Also even if assign the

user.val().name

to a variable, the variable remains undefined.Please help figure out why this happens. I am printing it to a csv.

Here is my code:

var database = firebase.database();
var ref2 = database.ref('information/');

var id;
var name;

ref2.on("value", function (one) {

    one.forEach(function (two) {

        if (typeof two.val().Id !== 'undefined') {
            id = two.val().Id;
            name = program(id); //name undefined

        }
        else {
           id = "";
        }

        csv = name + "," + id +"\n";


    });

    download(csv);

});
};


function program (id) {

var database = firebase.database();
var ref = database.ref("users/" + id + "/");

ref.on('value',function(user){

    if (typeof user.val().name === 'undefined') {
        return null;
    }
    else {
        console.log(user.val().name); //this doesnt show undefined
        return user.val().name; //this shows undefined when appended to a html element

    }
})
}

Note: In the firebase database, the name value is not null. It has a string added to it.

解决方案

I second with Frank's reason on why your function program() doesn't work. Because ref.on('value'... makes an asynchronous call, program() does not wait for the completion of ref.on and exists with an undefined return value.

What you could instead do is use Promises. Wrap the statements inside your program() function within a Promise, and upon completion of the asynchronous call, resolve or reject based on the result it gives.

Here's your function with Promises:

function program(id) {
    return new Promise(function (resolve, reject) {
        try {
            var database = firebase.database();
            var ref = database.ref("users/" + id + "/");

            ref.on('value', function (user) {
                if (typeof user.val().name === 'undefined') {
                    resolve(null);
                } else {
                    console.log(user.val().name);
                    resolve(user.val().name);
                }
            })
        } catch (e) {
            reject(e)
        }
    });
}

And then, here's how you can read the result:

program(id).then(function (result) {
        console.log(result)
        //Do what you want with the result here
    }).catch(function (error) {
        console.log(error)
})

Note: You're executing this block in a for-each statement. If you're using Promises, you'd also need to look into how to use Promises inside a loop. For reference, check Promise.all()

这篇关于Firebase的价值是不确定的,当它不应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆