node-restful icon indicating copy to clipboard operation
node-restful copied to clipboard

PUT an element to an array

Open raisen opened this issue 10 years ago • 4 comments

Given this model:

var Task = app.task = restful.model('task', mongoose.Schema({
    title: { type: 'string', require: true },
    body: { type: 'string', require: true },
    user_id: { type: 'ObjectId', ref: 'user', require: true },
    comments: [{
      body: { type: 'string', require: true },
      votes: {
        user_id: { type: 'ObjectId', ref: 'user' },
        score: { type: 'number' } 
      },
      author: { type: 'ObjectId', ref: 'user' },
    }],
  }))
  .methods(['get', 'delete', 'put', 'post' ]);

Is this possible to PUT a Comment to an existing Task?

I was looking to use an URL like /tasks/:id:/comments and PUT a comment object to it.

raisen avatar May 27 '14 08:05 raisen

Currently this isn't actually supported, but I want to :)

The correct verb in this case would be POST though

baugarten avatar May 29 '14 03:05 baugarten

Thanks.

Are you aware of any workaround? Perhaps defining a custom route?

raisen avatar May 30 '14 06:05 raisen

You should be able to do

Task.route('comments', 'post', function(req, res, next) { 
  // Do whatever you want here
})

baugarten avatar May 30 '14 13:05 baugarten

This is the PATCH REST method.

PUT replaces the whole document whereas PATCH just updates the field you provide. If you still need this wait for #152 to be merged otherwise could you please close this.

OmgImAlexis avatar Jan 29 '17 05:01 OmgImAlexis