当使用node.js的集合中存在某个值时,Firebase返回false值 [英] Firebase returns false value when there is a value present in the collection using node.js

查看:25
本文介绍了当使用node.js的集合中存在某个值时,Firebase返回false值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Firebase中验证一条记录,然后再使用node.js向发布请求中的用户注册

I'm trying to verify a record in Firebase before registering the user with post request using node.js

***** Node.js代码****

***** Node.js Code ****

function checkUserExist(email) {
    var userExist = false;
    userRef.orderByChild('email').equalTo(email).once('value').then(function (snapshot) {
        if (snapshot.exists()) {
            userExist = true;
            console.log(userExist); // Here the value is returned true
        }
    });
    return userExist;
}

app.post('/register', urlencodedParser, function (req, res) {

    var testUser = checkUserExist(req.body.reg_email);
    console.log('A: '+ testUser);
    // Here the value is returned false instead of true

    console.log(checkUserExist(req.body.reg_email), req.body.reg_email);
    // Here the value is returned false instead of true
    var newUserObj = {
        first_name: req.body.reg_first_name,
        last_name: req.body.reg_last_name,
        email: req.body.reg_email
    };

    userRef.push(newUserObj);

    res.render('pages/register', { reg_data: { post_data: req.body, is_success: created, is_user_exist: checkUserExist(req.body.reg_email) } });
    //console.log(users);
});

推荐答案

数据是从Firebase异步加载的.到您的 return userExist 运行时,数据尚未加载.

Data is loaded from Firebase asynchronously. By the time your return userExist runs, the data hasn't loaded yet.

使用一些日志记录语句最容易看到这一点:

This is easiest to see with some logging statements:

console.log('Before starting query');
userRef.orderByChild('email').equalTo(email).once('value').then(function (snapshot) {
  console.log('Got query results');
});
console.log('After starting query');

运行此代码时,将得到以下输出:

When you run this code, you get this output:

开始查询之前

开始查询后

获得查询结果

这可能不是您期望的顺序,但是它恰好解释了为什么您的方法返回错误值的原因:函数返回时尚未加载数据.

That is probably not the order you expected, but it explains precisely why your method returns the wrong value: the data hasn't been loaded yet by the time your function returns.

任何需要数据库中数据的代码,都必须在 then()回调内部,或者从那里调用.

Any code that needs the data from the database, must either be inside the then() callback, or be called from there.

处理异步API的常用方法是返回承诺:

The common way to deal with asynchronous APIs is to return a promise:

function checkUserExist(email) {
    var userExist = false;
    return userRef.orderByChild('email').equalTo(email).once('value').then(function (snapshot) {
        return snapshot.exists();
    });
}

现在您可以像这样调用 checkUserExist :

Now you can call checkUserExist like this:

checkUserExist(req.body.reg_email).then(function(userExists) {;
  console.log(userExists);
})

请注意,在这里, userExists then()回调中仅在内部中具有正确的值.

Note that here too, the userExists only has the correct value inside the then() callback.

如果您使用的是现代JavaScript环境,则可以使用 async / await 进行函数调用:

If you're on a modern JavaScript environment, you can use async/await for the function call:

let userExists = await checkUserExist(req.body.reg_email);
console.log(userExists);

请注意,这仍在幕后进行异步调用.

Note that this is still doing the asynchronous call behind the scenes.

对于刚接触异步API的开发人员来说,这是一个非常常见的问题.我强烈建议您查看以下一些先前的答案:

This is a very common problem for developers new to dealing with asynchronous APIs. I highly recommend you check out some of these previous answers:

这篇关于当使用node.js的集合中存在某个值时,Firebase返回false值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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