简体   繁体   English

NODE.JS、MONGOOSE、JAVASCRIPT - 来自 promise 的值甚至不返回 null 或 undefined

[英]NODE.JS, MONGOOSE, JAVASCRIPT - value from promise doesn't even return null or undefined

Good day, I would like to know the bug behind my code,美好的一天,我想知道我的代码背后的错误,

so first I have this db.js code:所以首先我有这个 db.js 代码:

/** check if may fb_id */
module.exports.checkfbid =
    async(model, query) => {
        return new Promise((resolve, reject) => {
            if (!query) {
                query = {};
            }
            console.log("pumasok sa checkfbid"+ JSON.stringify(query));
            console.log("pumasok sa checkfbid" + query);
            model.find(query)
                .then(res => {
                    console.log("hindi pa napapalitan: "+res);
                    if(null){
                        resolve(null);
                    }else{
                        console.log("untouched: " + res);
                        resolve(res);
                    }

            })
            .catch(err => {
                console.log("rejected si branch" + err);
                reject(err);
                console.log(err);
                throw err;
            });
    });
}

and this controller.js code和这个 controller.js 代码

const id_result = await usersessionDB.checkfbid(model, {fb_id: id});// store sa id_result
        console.log("id_result have: " + id_result);// log kung may nkukuha na laman
        if (id_result === null) {// kung null si id_result, create ka new user
              ...
            }
        } else {// else kung hindi null si id_result
              ...
        }

EDIT!编辑! Sample output of console.logs console.logs 的示例输出

样本输出

Please disregard the foreign, unreadable comments.请忽略外国的、不可读的评论。 As you can see, console.log("pumasok sa checkfbid"+ JSON.stringify(query));如您所见, console.log("pumasok sa checkfbid"+ JSON.stringify(query)); displays my query from the controller, but when it went inside the promise query, I used从控制器显示我的查询,但是当它进入承诺查询时,我使用了

console.log("hindi pa napapalitan: "+res);
                    if(null){
                        resolve(null);
                    }else{
                        console.log("untouched: " + res);
                        resolve(res);
                    }

It doesn't display thing!?它不显示东西!? I've been stuck for almost 3 hrs.我已经被困了将近 3 个小时。

Summing it up, my problem is how can I define if it's null or undefine if the value return by the promise doesn't return even null or undefine?总结一下,我的问题是如果承诺返回的值甚至不返回 null 或 undefine,我该如何定义它是 null 还是 undefine? Is it just blank?它只是空白吗? or is it the other way around, it's returning a value but what kind of value?或者相反,它正在返回一个值,但是什么样的值? blank?空白的? Thank you.谢谢你。

I'm not sure if you intended this, but you're running a conditional statement against null ( if(null) {... ) which will always produce a falsey response.我不确定您是否打算这样做,但是您正在针对null ( if(null) {... ) 运行条件语句,这将始终产生错误响应。

Did you mean if(!res) {... ?你的意思是if(!res) {...吗?

A better approach is to remove your if statement and use: resolve(res ? res : null)更好的方法是删除您的 if 语句并使用: resolve(res ? res : null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM