在 mongoose 请求之后,我得到了我的文档 doc
,这是查询的结果
这是使用的架构
var searchSchema = new mongoose.Schema({
original : String,
images : [String],
image: String
});
型号:
var searchModel = mongoose.model('Search', searchSchema);
使用的代码:
searchModel.findOne({original : input}, function (err, doc) {
if (err) {
console.log(err);
}
if (typeof doc !== "undefined") {
console.log(doc);
console.log(doc.image);
}
});
第一个console.log
:
{
_id: 531401bf714420359fd929c9,
image: 'http://url.com/image.jpg',
original: 'lorem ipsum dolor sit amet'
}
第二个返回undefined
,但前一个确实显示了一个现有的image
属性,这意味着它存在。
我的架构没有什么特别之处,所以我不明白这里可能发生了什么..
最佳答案
当您尚未将字段添加到架构中时,您会看到这一点。
将 image
添加到您的架构中,它应该可以工作:
image: String
关于javascript - Mongoose 为现有字段返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22139113/