创build一个文档并添加到相同的路线设置

我有以下路线:

app.post('/accounts', (req, res) => { obj = new ObjectID() var account = new Account({ name: req.body.name, _owner: req.body._owner } ) return account.save() .then((doc) => { Account.update( { "_id": account._id }, { $addToSet: { subscriptions: obj } } ) }) .then((doc) => { res.send(doc) } ) }); 

我正在尝试创build一个文档,然后使用创build的对象ID更新它(数组)中的一个字段。 当我调用这个路由时,新的文档被创build,但是新的objectID没有被添加到订阅集中。

这是我的模型:

 var Account = mongoose.model('Account', { name: { type: String, required: true, minlength: 1, trim: true }, _owner: { type: mongoose.Schema.Types.ObjectId, required: true }, subscriptions: [{ type: mongoose.Schema.Types.ObjectId, required: true }] module.exports = { Account }; 

如果你已经find了id和订阅是数组然后

  app.post('/accounts', (req, res) => { obj = new ObjectID() var account = new Account({ name: req.body.name, _owner: req.body._owner }); return account.save() .then((doc) => { return Account.update( //return was missing which was causing the issue because of promise chain { "_id": account._id }, { $addToSet: { subscriptions: obj } } ) }).then((doc) => { res.send(doc) } ) });