Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waterline: Populate model related object on create (Sails)

    Token.create({
      type: 'invite-into-org',
      email: email,
      initiator: organization,
      sender: req.user,
      metadata: {
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        role: req.body.role
      }
    }).exec(function(err, token) {
      if(err) { return callback(err); }
        console.log("THE TOKEN", token)
      callback(null, token);
    });

Prints to console the following content:

THE TOKEN { type: 'invite-into-org',
19:14:39 web.1 |    email: '[email protected]',
19:14:39 web.1 |    initiator: 8,
19:14:39 web.1 |    sender: 9,
19:14:39 web.1 |    metadata: { firstName: 'Vasyl', lastName: 'Romanchak', role: 'author' },
19:14:39 web.1 |    createdAt: '2015-07-03T16:14:39.964Z',
19:14:39 web.1 |    updatedAt: '2015-07-03T16:14:39.964Z',
19:14:39 web.1 |    id: '5596b4efe4eccd7b519eedef' }

Is there any way to populate initiator & sender fields ?

Token.create({}).populate('sender').exec(console.log) - still the same

like image 735
vromanch Avatar asked Sep 27 '22 10:09

vromanch


1 Answers

My solution use dumbest way that re-query the result, but it use Promise to get better readability.

Token
  .create({
    type     : 'invite-into-org',
    email    : email,
    initiator: organization,
    sender   : req.user,
    metadata : {
      firstName: req.body.firstName,
      lastName : req.body.lastName,
      role     : req.body.role
    }
  })
  .then(function (token) {
    return Token.findOne({id: token.id}).populateAll();
  })
  .then(function (record) {
    console.log('THE TOKEN', record);

    callback(null, record);
  })
  .catch(callback);
like image 169
Andi N. Dirgantara Avatar answered Oct 04 '22 19:10

Andi N. Dirgantara