Mongoose fineOne不起作用但发现确实如此

时间:2016-04-22 00:58:03

标签: node.js mongodb mongoose

findOne在结果中未定义:

Upload.findOne({_id: req.params.id}, function (err, upload) {
  if (err || !upload) return;
  console.log(upload.name); /* Empty string */
});

然而,使用find会得到结果:

Upload.find({_id: req.params.id}, function (err, upload) {
  upload = upload[0];
  if (err || !upload) return;
  console.log(upload.name); /* it prints out what is expected */
});

我在两种情况下都确认mongo查询成功运行,并且通过mongoDB shell都返回了预期的结果:

Mongoose: uploads.findOne({ _id: ObjectId("57196fced4595c2a09146891") }) { fields: undefined }
Mongoose: uploads.find({ _id: ObjectId("57196f0a8d2055520894d5a4") }) { fields: undefined }

猫鼬模型:

const UploadSchema = new Schema ({
  name: {type: String, default: '', trim: true},
  mimetype: {type: String, default: '', trim: true}
});

UploadSchema.path('name').required(true, 'Name cannot be blank.');
UploadSchema.path('mimetype').required(true, 'Mimetype cannot be blank.');

module.exports = mongoose.model('Upload', UploadSchema);

我做错了什么?

节点版本:5.10。 1,猫鼬版:4.4.10

提前致谢!

0 个答案:

没有答案