将 MongoDB 中的数据存储在变量中 - mongoose 和 node.js
我想知道是否有人可以向我展示如何从 mongodb 获取字符串值并将其存储在变量中。
db.model("users").findOne({username: username, password: password}, {type:1},
function(err, data) {
if(err) {
return "Error";
} else if() {
return "No records";
} else {
return data.type;
}
}
);
当我运行这个时,我得到了类型值。
但是如何将其存储在该函数外部的变量中呢?
I was wondering if someone could show me how to get a string value from mongodb and store that in a variable.
db.model("users").findOne({username: username, password: password}, {type:1},
function(err, data) {
if(err) {
return "Error";
} else if() {
return "No records";
} else {
return data.type;
}
}
);
I get the type value when I run this.
But how do I store this in a variable outside this function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
匿名函数的返回值被完全忽略。您可以将变量存储在当前作用域可访问的任何位置。例如
,您可以从 server.js 中调用:
您需要巧妙地了解不同元素如何交互,因为带有回调的 Javascript 不会同步运行。也就是说,应该有可能实现您想要的任何结果。享受编码带来的快乐!
The return value from your anonymous function is completely ignored. You can store the variable anywhere that is accessible from your current scope. E.g.
From server.js, you could then call:
You will need to be clever about how different elements interact since Javascript with callbacks does not run synchronously. That said, it should be possible to achieve any result you are looking for. Enjoy and happy coding!