【发布时间】:2020-12-25 13:51:05
【问题描述】:
我正在使用 mongoose 5.9.28 和节点 v12.16.1。
我需要编写一个函数,它将一个列表作为参数并在猫鼬模型中填充该列表。 (模型在函数中是常数)
我的架构:
var schema = new mongoose.Schema({
id : {
type : String,
required : true,
unique : true,
},
driverId : {
type : mongoose.Schema.Types.ObjectId,
ref : "drivers"
},
vehicleId : {
type : mongoose.Schema.Types.ObjectId,
ref : "vehicles"
},
customerId : {
type : mongoose.Schema.Types.ObjectId,
ref : "customers",
required : true
},
bookedOn : {
type : String
},
pickUpLocation : {
type : String,
required : true,
},
dropLocation : {
type : String,
required : true
},
paymentId : {
type : mongoose.Schema.Types.ObjectId,
ref : "payments"
},
bookingStatusId :{
type : mongoose.Schema.Types.ObjectId,
ref : "booking_status"
},
goodsType : {
type : String,
required : true
}
});
这里的driverId、vehicleId、customerId、paymentId、bookingStatusId是对其他模型的引用。
我有这个函数,其中 refs 是一个列表。
const getBookings = async (refs) => {
const booking = await bookingModel.find().lean().populate({
path : refs,
select : ['-_id']
).exec()
return booking;
}
如果我打电话给getBookings(['customerId','driverId']),我应该得到包含客户和驱动程序详细信息的文档,不包括_id。
但我得到的错误是TypeError: utils.populate: invalid path. Expected string. Got typeof "object"。
任何帮助将不胜感激。提前致谢
【问题讨论】:
标签: javascript node.js mongodb mongoose mongoose-populate