After a mongoose request I have my document doc
which is the result of the query
Here is the schema used
var searchSchema = new mongoose.Schema({
original : String,
images : [String],
image: String
});
The model :
var searchModel = mongoose.model('Search', searchSchema);
Code used:
searchModel.findOne({original : input}, function (err, doc) {
if (err) {
console.log(err);
}
if (typeof doc !== "undefined") {
console.log(doc);
console.log(doc.image);
}
});
The first console.log
:
{
_id: 531401bf714420359fd929c9,
image: 'http://url.com/image.jpg',
original: 'lorem ipsum dolor sit amet'
}
The second returns undefined
, but the previous one does show an existing image
property, which means that it exists.
My schema doesn't have anything special so I don't understand what may be happening here..
You'll see this when you haven't added the field to your schema.
Add image
to your schema and it should work:
image: String
This is do to the fact that the toString() method of the object returns the _doc property. You can use: console.log(doc._doc.image);
我遇到了同样的错误,但是像这样访问属性doc[0].image
工作正常。
Try to use the bracket notation like that:
doc['image']
If it works, I'm not able to explain you why, but maybe someone could shed some light on this?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.