javascript - 想检查用户是否已经存在 MongoDB
问题描述
我的承诺总是拒绝即使我传入参数的_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!")
}
}
)
推荐阅读
- sql - SQL Server:选择记录,不链接到另一个表
- angularjs - AngulaJS 计算匹配多个字符串的过滤结果
- powershell - 如何删除路径的“基础”部分?
- python - 如何使用 Selenium 和 Python 打开 Microsoft Edge
- python - 识别列表中的所有重叠元组
- postgresql - 如何更改 postgresql 数组的默认分隔符?
- vb.net - 如何在 vb.net 中创建级联组合框
- html - Webscrape 使用 BeautifulSoup 到 Dataframe
- bazel - Bazel WORKSPACE 有条件地定义了两个`git_repository`s之一
- java - 如何将 CombineFn 与 getKey 一起使用