我有一个‘供应商’和‘媒体’的收集,媒体收集商店供应商的个人资料图像,这不是必须的领域供应商。有些供应商有个人资料图片有些没有,我想列出所有供应商,包括他们的个人资料图片参考。但是,当我从meidas中填充profilePicture时,我会得到强制转换错误。我还有其他填充程序,如'userId‘、'venueId’--这些都是强制字段,而且可以工作。
错误: CastError:对于未定义的模型"medias“路径"_id”中的值"xyz“,强制转换为ObjectId失败
查找查询:
const result = await resultObject.skip(skip)
.populate('userId')
.populate('venueId')
.populate('profilePicture')
.limit(parseInt(obj.pageSize, 10))
.sort({ [obj.sortColumn]: parseInt(obj.sortValue, 10) });
return result;介质模型
const MediasModel = () => {
const MediasSchema = new mongoose.Schema({
url: { type: String, required: true },
key: { type: String},
}, { timestamps: { createdAt: 'createdDate', updatedAt: 'modifedDate' } });
return mongoose.model('medias', MediasSchema);
};
module.exports = MediasModel();供应商模型
const Vendors = new mongoose.Schema({
venueId: { type: mongoose.Schema.Types.ObjectId, ref: 'venues' },
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'users' },
name: { type: String, required: true },
profilePicture: { type: mongoose.Schema.Types.ObjectId, ref: 'medias' },
}, { timestamps: { createdAt: 'createdDate', updatedAt: 'modifedDate' } });
module.exports = mongoose.model('vendors', Vendors);发布于 2019-11-20 04:52:36
试试下面的代码
const Vendors = require('path/to/model_vendor');
Vendors.find()
.populate([{path: 'venues'}, {path: 'users'}, {path: 'medias'}])
.skip(skip_value)
.limit(limit_value)
.sort({condition: 1 }) // Modify according to your need
.exec().
.then(vendors=>{
console.log(vendors)
}).catch(err=>{
console.log(err)
})https://stackoverflow.com/questions/58945126
复制相似问题