创建文档并添加到相同路径中设置

时间:2017-02-13 14:39:16

标签: node.js mongodb express mongoose

我有以下路线:

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)
    }
  )
});

我正在尝试创建一个文档,然后用创建的objectID更新它(数组)中的字段。当我调用此路由时,将创建新文档,但新的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
};

1 个答案:

答案 0 :(得分:3)

如果您发现id和subscriptions是数组,那么

const wrapper = shallow(<connectedFeature {...props} store={store} />).shallow()