我正在更新从Model.create()到Model.insertMany()的路由
但是在insterMany()之后尝试关联ref给我一个错误
我在Program.js
中有一个带有优惠券参考的程序模型:
const ProgramSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
imageUrl: {
type: String
},
url: {
type: String,
required: true
},
partner: {
type: String,
required: true
},
coupon: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'coupon'
}
]
});
module.exports = Program = mongoose.model('program', ProgramSchema);
Coupon.js
中的优惠券模型:
const CouponSchema = new mongoose.Schema({
number: {
type: String,
required: true
}
});
module.exports = Coupon = mongoose.model('coupon', CouponSchema);
最初是我的路线:
// @route POST /:id/coupon
// @desc Generate new random coupons for a particular program
// @access Private
router.post('/:id/coupon', (req, res) => {
Program.findById(req.params.id, (err, program) => {
if (err) {
console.error(err);
} else {
// Get the number of coupons to generate
let couponNum = req.body.number;
// Generate coupons
let couponsData = generateCoupons(couponNum);
// Create as many coupons is passed in the request
Coupon.insertMany(couponsData, (err, coupons) => {
if (err) {
console.error(err);
} else {
console.log(coupons);
// connect newlly created coupons to Program model
program.coupon.push(coupons);
console.log(program);
program.save();
res.redirect('/programs/' + program._id); // redirect to the program page
}
});
}
});
});
输出(将优惠券添加到优惠券模型中的数据库中,而不是添加到程序模型中,并返回此错误):
CastError: Cast to ObjectId failed for value "[ { _id: 5c9a339acc2ea92fa0f5293e,
number: '4B7FE4C90FE942E7',
__v: 0 },
{ _id: 5c9a339acc2ea92fa0f5293f,
number: 'E67B454B2CEE45CE',
__v: 0 },
{ _id: 5c9a339acc2ea92fa0f52940,
number: 'EFDB1E7FE4CC470C',
__v: 0 },
{ _id: 5c9a339acc2ea92fa0f52941,
number: '913BA9747957470C',
__v: 0 },
{ _id: 5c9a339acc2ea92fa0f52942,
number: '5DC51728E5F04512',
__v: 0 } ]" at path "coupon"
在搜索过猫鼬的文档之后,findByIdAndUpdate()解决了该错误,但是新的优惠券替换了那些已经存在的优惠券,这不是预期的行为。我希望添加它而不覆盖已经存在的那些
新代码:
// @route POST /:id/coupon
// @desc Generate new random coupons for a particular program
// @access Private
router.post('/:id/coupon', (req, res) => {
Program.findById(req.params.id, (err, program) => {
if (err) {
console.error(err);
} else {
// Get the number of coupons to generate
let couponNum = req.body.number;
// Generate coupons
let couponsData = generateCoupons(couponNum);
// Create as many coupons is passed in the request
Coupon.insertMany(couponsData, (err, coupons) => {
if (err) {
console.error(err);
} else {
console.log(coupons);
// connect newlly created coupons to Program model
Program.findByIdAndUpdate(
program._id,
{ coupon: coupons },
(err, program) => {
if (err) {
console.error(err);
} else {
console.log(program.coupon);
}
}
);
res.redirect('/programs/' + program._id); // redirect to the program page
}
});
}
});
});
输出(没有错误,已成功添加,但覆盖了现有的优惠券):
[ { _id: 5c9a379c5cace31c289e2c40,
number: '7E29798D4AB34C31',
__v: 0 },
{ _id: 5c9a379c5cace31c289e2c41,
number: 'D973E23A4E9D46F2',
__v: 0 },
{ _id: 5c9a379c5cace31c289e2c42,
number: '6D50BBB802524EAB',
__v: 0 },
{ _id: 5c9a379c5cace31c289e2c43,
number: '8D78C0A05ADB4AA0',
__v: 0 },
{ _id: 5c9a379c5cace31c289e2c44,
number: 'CDC4C282E2C648CD',
__v: 0 } ]
那么在insertMany()查询之后关联引用的最佳方法是什么?谢谢您的宝贵时间。