//出现此错误{ “错误”:{ “message”:“对于模型\”用户\“”,对于路径\“_ id \”,对于值\“events \”,转换为ObjectId失败, “name”:“CastError”, “stringValue”:“\”events \ …
我认为你填写事件文件有点不对劲。
试试这个:
Event.find({}) .populate("creator._id","name _id") .then(result => { console.log(result); res.status(200).json({ result }); }).catch(err => { console.log(err); res.status(500).json({ error: err }); });
我不认为你需要任何第三个论点 .populate() 功能,你已经在你的定义 schema ,它应该从以下位置填充:
.populate()
schema
//look here, you have already defined it in your schema creator: { _id: { type: mongoose.Schema.Types.ObjectId, ref: "users" //this says which collection it should be populated from } }
我希望它可以帮助你。