1

I have sails js 0.11v and mongodb, when i use .update function of sails js, Except enum attrribute every other value is updating.

Here is my model

module.exports = {
    schema: true,
    attributes: {
        name: {
            type: 'string',
            required: true
        },
        type: {
            type: 'string',
            required: true,
            enum: ['open', 'user']
        },

        state: {
            type: 'integer',
            enum: [0, 1, 2, 3, 4, 5, 6],
            defaultsTo: 0

        }

    }

};

Below code is responsible for updating values

Challenge.update({
    id: '55e81f5998e63e7c1ebe4ab6'
}, {
    state: 2
}, {
    type: 'user'
}).exec(function afterwards(err, updated) {
    if (err) {
        return res.json(err);
    } else {
        return res.json(updated);
    }
});

but it dont update type and state parameter in challenge. It results old data

sgress454
  • 24,870
  • 4
  • 74
  • 92
rash111
  • 1,307
  • 4
  • 19
  • 35
  • 3
    You are using separate objects for each field when they need to be all in the same object. That's why it fails. i.e `{ "state": 2, "type": "user" }` – Blakes Seven Sep 11 '15 at 07:46
  • 1
    Umm. That's because the "first" document is the query and the "second" is the update. You are only meant to have "two". You have "three". Use what I told you as the "second". – Blakes Seven Sep 11 '15 at 09:20
  • Thanks For help, It solve my problem – rash111 Sep 11 '15 at 09:20
  • @BlakesSeven even though this is a bit of RTFM, might as well post your comment as an answer in case someone else is stumped doing the same thing. – sgress454 Sep 11 '15 at 15:53
  • @sgress454 The initial comment was pointing out the syntax error. The somewhat concerning now deleted comment poitned to an example from sails documentation that of course only updated a single field as the justification for adding an addional "object" to the update. So the lack of example or attribution to the core documentation is a concern. Somewhat related to the featured [Warlords of Documentation](http://meta.stackoverflow.com/questions/303865/warlords-of-documentation-a-proposed-expansion-of-stack-overflow?cb=1) proposal, as if things are really that bad, then a finer manual is needed – Blakes Seven Sep 12 '15 at 03:26

1 Answers1

0

Actually i had the same issue a couple of minutes ago, the problem is that .update() accepts only 2 params, so you could edit according to the following:

Challenge.update({id:'55e81f5998e63e7c1ebe4ab6'}, {state: 2,type: 'user'}).exec(function afterwards(err, updated) { if (err) { return res.json(err); } else { return res.json(updated); } });

And it works!

Michi Salazar
  • 136
  • 2
  • 6