【发布时间】:2018-07-02 10:35:05
【问题描述】:
我正在使用快递、护照和猫鼬。我不知道为什么,但下面的代码将相同的 newTaxReturn._id 两次推送到 user.taxReturnIds 字段中。如果我删除user.save().catch(() => {}) 行,它会正确推送newTaxReturn._id,即只推送一次。用户参数来自护照。
问题:
const createTaxReturn = ({ user }) => {
const newTaxReturn = new TaxReturn({ userId: user._id })
user.taxReturnIds.push(newTaxReturn._id)
user.save().catch(() => {})
return newTaxReturn.save().catch(() => {})
}
架构:
const User = new mongoose.Schema({
taxReturnIds: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'TaxReturn',
}],
})
const TaxReturn = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
})
【问题讨论】:
标签: javascript node.js express mongoose passport.js