node.js - Mongoose fineOne does not work but find does -
findone gets undefined in result:
upload.findone({_id: req.params.id}, function (err, upload) { if (err || !upload) return; console.log(upload.name); /* empty string */ });
however using find instead result:
upload.find({_id: req.params.id}, function (err, upload) { upload = upload[0]; if (err || !upload) return; console.log(upload.name); /* prints out expected */ });
i confirmed in both cases mongo query run , through mongodb shell both returned expected result:
mongoose: uploads.findone({ _id: objectid("57196fced4595c2a09146891") }) { fields: undefined } mongoose: uploads.find({ _id: objectid("57196f0a8d2055520894d5a4") }) { fields: undefined }
mongoose model:
const uploadschema = new schema ({ name: {type: string, default: '', trim: true}, mimetype: {type: string, default: '', trim: true} }); uploadschema.path('name').required(true, 'name cannot blank.'); uploadschema.path('mimetype').required(true, 'mimetype cannot blank.'); module.exports = mongoose.model('upload', uploadschema);
what doing wrong?
node version: 5.10. 1, mongoose version: 4.4.10
thanks in advance!
Comments
Post a Comment