Monday, 1 May 2017

Mongoose how to add an object in an embedded array ?

I am trying to add a new Permission object in an Permissions array this way

MODEL const Permission = new Schema({ permission: { role_id: { type: String, required: false }, canWrite: { type: Boolean, required: true, default: false } } });

    const TableSchema = new mongoose.Schema({
      meta: {
        ...
        permissions: [Permission],
        ...

TEST const newReadPermission = { permission: { role_id: '56z787zzz67fc', canWrite: false } };

     .post(`/api/v1/tables/${tablePrivate._id}/permissions`)
            .send(newReadPermission)
            .expect(httpStatus.OK)

CONTROLLER

    function createPermission(req, res, next) {
      const query = { _id: req.table.id };
      const update = { $push: { meta.permissions: req.body } };
      const options = { new: true };
      Table.findOneAndUpdate(query, update, options)
        .then(savedTable => res.json(savedTable))
        .catch((e) => {
          console.log('ERROR: ', e);
          next(e);
        });
    }

in the $push meta.permissions: property is not accepted... what should I use instead ?

thanks for feedback



via erwin

No comments:

Post a Comment