我的 promise 总是拒绝,即使我传入参数的_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!")
}
}
)
});
};
最佳答案
如果找到数据,您必须解决 promise 并拒绝 promise 。我已更正您的代码如下:
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!")
}
}
)
关于javascript - 想要检查用户是否已经存在 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149008/