【发布时间】:2020-10-28 22:08:59
【问题描述】:
我能够连接到我的 MongoDB 数据库,并且确信我正在连接到正确的数据库。当我尝试从数据库中获取文档时,我总是得到“null”。我确信集合中有文档,因为我可以在 Compass 中看到它们。
const theschema = new Schema({
sensor: {
type: String,
required: true
}
})
const model = mongoose.model('Sensor', theschema)
model.findOne({}, function(err, data) {
console.log(data)
})
以上是我目前使用的代码。我将不胜感激任何可能的帮助。谢谢! :)
【问题讨论】:
-
您是否检查过
err是否已定义?当你从 mongodb 读取时,可能有一些错误。 -
您的收藏名称是什么,您如何连接到它?
标签: javascript node.js mongoose mongoose-schema