要检查用户是否已经存在MongoDB

法兰基·文森特

我的诺言总是拒绝即使我传递的参数_name与集合中的_name都不总是相同的,任何帮助将不胜感激,谢谢!

    myCheckUser(_name) {
        var self = this;
        return new Promise((resolve, reject) => {
            self.db.collection("USER").find({ "username": _name }, { $exists: true }).toArray(function    (err, doc) //find if a value exists
            {
                console.log("DOC USERNAME: " + doc.username);
                if (doc) //if it does
                {
                    reject("Found user");
                    console.log(doc.username); // print out what it sends back
                }
                else // if it does not 
                {
                    console.log("Not in docs");
                    resolve("Not found continue logic!")
                }
            }
            )
        });
    };

维卡什·辛格

如果找到数据,您必须解决承诺并拒绝承诺。我已更正以下代码:

    myCheckUser(_name) {
        var self = this;
        return new Promise((resolve, reject) => {
            self.db.collection("USER").find({ "username": _name }, { $exists: true }).toArray(function    (err, doc) //find if a value exists
            {
                if (doc && doc.length) //if it does
                {
                    console.log(doc); // print out what it sends back
                    resolve("Found user");
                }
                else // if it does not 
                {
                    console.log("Not in docs");
                    reject("Not found continue logic!")
                }
            }
            )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章