Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected results from Model.create #566

Closed
OscarGodson opened this issue Jul 12, 2013 · 1 comment
Closed

Unexpected results from Model.create #566

OscarGodson opened this issue Jul 12, 2013 · 1 comment

Comments

@OscarGodson
Copy link

Lets say I have:

  create: function (req, res) {
    Model.create({
      title: req.param('title'),
      value: req.param('value'),
    }).done(function (err, model) {
      if (err) {
        res.send(500, {error: 'DB error'});
      }
      else {
        res.send(model);
      }
    });
  }

It returns

{
    "title": "Hello world",
    "value": 12,
    "createdAt": "2013-07-12T08:04:57.297Z",
    "updatedAt": "2013-07-12T08:04:57.297Z",
    "id": 2,
    "values": {
        "title": "Hello world",
        "value": 12,
        "createdAt": "2013-07-12T08:04:57.297Z",
        "updatedAt": "2013-07-12T08:04:57.297Z",
        "id": 2
    }
}
  1. Is this expected for you guys?
  2. Why is the data repeated in a values property? What's the "value" of that (see what I did there)?
  3. If 1 is true, should I always res.send(model.values)?
@particlebanana
Copy link
Contributor

This is the expected behavior in the current version 0.8.x. You are correct that you should use res.send(model.values) however this has been fixed in 0.9.x which should be out Monday.

In 0.9.x you can just call res.send(model) and it will return only the attributes. Or if you need to filter the attributes being returned you can write a toJSON instance method on your model and use res.send(model.toJSON()) more info on that is: https://github.com/balderdashy/sails-wiki/blob/0.9/models.md#toobjecttojson-instance-methods

I'd say use res.send(model) for now so you don't need to worry about updating that for 0.9.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants