我的诺言总是会拒绝,即使我传入的_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!")
}
}
)
});
};
答案 0 :(得分:2)
如果找到数据,您必须解决承诺并拒绝承诺。我已在下面更正了您的代码:
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!")
}
}
)