【发布时间】:2019-11-09 04:26:19
【问题描述】:
1.我正在尝试通过节点 below.js 查询数据库“teamautomation”中的集合“radar_life_cycle”,如下所示
2.我创建了对应的模型 ,通过mongoose连接mongodb成功,没有错误但我得到一个空列表。
- 从帖子下面的输出中可以看到,
[]是空列表,但我可以看到数据库中有记录,谁能提供有关此处可能出现问题的指导?
app.js
const express = require("express");
const mongoose = require("mongoose");
const Radar_life_cycle = require("./models/radar_life_cycle");
const app = express();
mongoose
.connect(
"mongodb://username:password@1x.xxx.xxx.x:27017/wifiautomation"
)
.then(() => {
console.log("Connected to database!");
})
.catch(() => {
console.log("Connection failed!");
});
app.get("/api/radars", (req, res, next) => {
Radar_life_cycle.find({ orgRadar: "51918661" }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
模型/radar_life_cycle
const mongoose = require('mongoose');
const radar_life_cycle_Schema = mongoose.Schema({
Delivered: String,
orgRadar: String,
root_build: String,
inserted_by: String,
milestone: String,
applicable_chipsets: [String],
project_tag: String,
gerrits:String,
inserted_on:Date,
SDK:String
});
module.exports = mongoose.model('radar_life_cycle', radar_life_cycle_Schema);
输出:-
terminal$ nodemon server.js
[nodemon] 1.14.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
(node:17500) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Connected to database!
{"message":"Posts fetched successfully!","posts":[]}
预期输出:-
{"message":"Posts fetched successfully!","posts":should include the corresponding records}
数据库中的文档截图:
【问题讨论】:
-
你确定你的数据库中有文档吗,因为代码看起来很完美。
-
@RaviShankarBharti - 是的,我确定数据库中有记录,我用它的屏幕截图更新了我的问题,可能有什么问题?
-
您是否尝试过将集合名称作为第三个参数传递(mongoose.model('radar_life_cycle',radar_life_cycle_Schema,'radar_life_cycle'))? Mongoose 会在集合名称中添加一个“s”,这可能是您的问题的根源。
标签: node.js mongodb mean-stack