Node.js mongoose无法使用默认值empty object填充ref id
我的架构如下所示:Node.js mongoose无法使用默认值empty object填充ref id,node.js,mongodb,Node.js,Mongodb,我的架构如下所示: const order = new Schema({ order_status: Number, foodtruck_id: { type: Schema.Types.ObjectId, ref: 'foodtruck' }, customer_id: { type: Schema.Types.ObjectId, ref: 'user' }, items: [{ type: Schema.Types.ObjectId, ref: 'items
const order = new Schema({
order_status: Number,
foodtruck_id: { type: Schema.Types.ObjectId, ref: 'foodtruck' },
customer_id: { type: Schema.Types.ObjectId, ref: 'user' },
items: [{ type: Schema.Types.ObjectId, ref: 'items' }],
user_type: Boolean,
order_time: Date,
order_rating: { type: Number, default: 5.0 },
order_issue_comments: String,
order_special_instruction: String,
order_total: Number,
order_location: String,
order_coupon_code: String,
payment_id: { type: Schema.Types.ObjectId, ref: 'payment' },
order_meta: { type: Schema.Types.Mixed, ref: 'order_sub_info', default: {} }
}, { versionKey: false }, { minimize: false });
order.find({
'foodtruck_id': foodtruck_id.trim()
}).populate('customer_id', {
'_id': 1,
'user_name': 1,
'email_id': 1,
'ph_no': 1,
'login_type': 1
}).populate('items').
populate('order_meta', 'order_otp').exec((err, orderList) => {
if (err) res.json({
status: '500',
message: err
});
else {
console.log("called");
res.json({
status: '200',
message: 'Order list',
data: orderList
});
}
});
我的查询如下:
const order = new Schema({
order_status: Number,
foodtruck_id: { type: Schema.Types.ObjectId, ref: 'foodtruck' },
customer_id: { type: Schema.Types.ObjectId, ref: 'user' },
items: [{ type: Schema.Types.ObjectId, ref: 'items' }],
user_type: Boolean,
order_time: Date,
order_rating: { type: Number, default: 5.0 },
order_issue_comments: String,
order_special_instruction: String,
order_total: Number,
order_location: String,
order_coupon_code: String,
payment_id: { type: Schema.Types.ObjectId, ref: 'payment' },
order_meta: { type: Schema.Types.Mixed, ref: 'order_sub_info', default: {} }
}, { versionKey: false }, { minimize: false });
order.find({
'foodtruck_id': foodtruck_id.trim()
}).populate('customer_id', {
'_id': 1,
'user_name': 1,
'email_id': 1,
'ph_no': 1,
'login_type': 1
}).populate('items').
populate('order_meta', 'order_otp').exec((err, orderList) => {
if (err) res.json({
status: '500',
message: err
});
else {
console.log("called");
res.json({
status: '200',
message: 'Order list',
data: orderList
});
}
});
对于这个查询,它为我提供了路径_id处的值的
转换为ObjectId失败,因为顺序_meta
具有默认值{}。如何有效地填充查询,以便它能够处理这个测试用例?将空对象放在需要引用id的地方不是一个好主意。两者都有-对于填充和常识也有问题(如果是有引用的字段,则应为null/未定义或引用本身)
通常,您希望在某个端点转换数据,但它不应干扰应用程序的数据库或业务逻辑
您可以定义用于模型的toJSON方法。就你而言
const order = new Schema({
order_status: Number,
foodtruck_id: { type: Schema.Types.ObjectId, ref: 'foodtruck' },
customer_id: { type: Schema.Types.ObjectId, ref: 'user' },
items: [{ type: Schema.Types.ObjectId, ref: 'items' }],
user_type: Boolean,
order_time: Date,
order_rating: { type: Number, default: 5.0 },
order_issue_comments: String,
order_special_instruction: String,
order_total: Number,
order_location: String,
order_coupon_code: String,
payment_id: { type: Schema.Types.ObjectId, ref: 'payment' },
order_meta: { type: Schema.Types.ObjectId, ref: 'order_sub_info'}
}, { versionKey: false }, { minimize: false });
order.options.toJSON = {
transform(zipRequestDocument, ret, options) { // eslint-disable-line no-unused-vars
if (!ret.order_meta){
ret.order_meta = {};
}
},
};
你到底想干什么?如果要填充,则需要ObjectId,而不是Mixed。此外,空对象也没有意义-它要么为null/undefined,要么为acutal reference。在前端,如果没有条目,但没有null
或undefined
,则我希望大小为0的order\u meta
。为了处理这个测试用例,我使用了Mixed
typeWell,这与从数据库导出的数据有关,因此您不应该将这个逻辑放入模型中。您可以重写toJSON方法,在这里您可以使用“if”null,然后将其设置为空对象,然后在将其发送到前端后,它将以这种方式序列化。