我正在尝试使用以下代码填充对象数组:
inventory.populate(result, {
path: 'activities.mean',
$match: { 'activities.mean': {$ne: ''} }
}, callback);
其中 mean
的类型是:
mean:{type:String, ref: 'Inventory'}
在填充结果时,我在回调函数中得到错误:
CastError:对于模型“Inventory”的路径“_id”,对于ObjectId的转换为失败“...”
这清楚地表明我的结果包含空 activities.mean
.
我尝试了不同的解决方案,包括我在上面的代码中写的 match
,但我无法真正使它工作 . 不知道为什么 match
在这里不起作用 .
What I'm expecting this code to do is:
如果 activities.mean
为空字符串,则不要尝试填充 mean
.
1 回答
更多看起来问题是您使用
String
类型作为引用字段 - 因为mongoose正在尝试将字符串值转换为通常用于引用的ObjectId . 它应该是Mongoose的documentation本身就注意到了这一点