node.js - findOne() 查询在 mongo 控制台上运行良好,但相同的查询在我的 node.js 代码中没有返回任何内容
问题描述
我正在开发一个带有 mongo 数据库和 node.js 的简单问答系统。
如果用户发送一个预期的问题,系统应该找到它并给出一个相关的答案。
我在 mongo 控制台中尝试查询的第一个操作是:
db.answers.findOne({"question": "theQuestion"})
此查询返回与该问题匹配的文档。
当我从 node.js 尝试相同的查询时,没有响应。
MongoClient.connect(MONGO_PATH, function (err, client) {
let db = client.db("fierobot");
db.collection("answers").findOne({
question: "theQuestion"
}, function (error, response) {
if (error)
throw error;
if (response)
console.log(response);
else
console.log("NO RESPONSE"); // <-- I always get this
});
});
这是我应该收到的:
{
"_id" : ObjectId("5d27211a8bd7a75659148866"),
"question" : "theQuestion",
"answer" : "theAnswer"
}
解决方案
MongoClient.connect(MONGO_PATH, function (err, client) {
let db = client.db("fierobot");
db.collection("answers").findOne({
question: { $eq: "theQuestion" }
}, function (error, response) {
if (error)
throw error;
if (response)
console.log(response);
else
console.log("NO RESPONSE"); // <-- I always get this
});
});
推荐阅读
- arduino - 使用 Arduino Nano 的串行接口与 ESP8266 通信——当前挂起
- sql-server - TSQL如何将连接表中的列与group by连接起来
- c - 在 C 中读取二进制有符号短问题
- c - 编译器为限制指针指向的内存而采取的措施
- scala - 是否将隐式执行上下文传递给 .par 操作?
- ibm-mq - IBM MQ 是否有任何未解决的错误列表?
- javascript - 日期验证(日期不应在现有日期范围内)
- r - 如何基于 R 中长格式的数据框创建具有不同列长度的 Markdown 表?
- html - 纯 CSS 范围滑块,其背景颜色根据进度/值而变化
- android - 如何将android模拟器配置为在旋转时自动改变方向