mysql - Waterline:在创建时填充模型相关对象(Sails)

标签 mysql mongodb sails.js waterline

    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);
    });

打印到控制台以下内容:

THE TOKEN { type: 'invite-into-org',
19:14:39 web.1 |    email: 'somoone@gmail.com',
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' }

有没有办法填充发起者和发送者字段?

Token.create({}).populate('sender').exec(console.log) - 还是一样

最佳答案

我的解决方案使用最愚蠢的方式重新查询结果,但它使用 Promise 来获得更好的可读性。

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);

关于mysql - Waterline:在创建时填充模型相关对象(Sails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31210967/

相关文章:

mysql - Access 查询分组/求和

javascript - 了解 Mongoose 子文档

reactjs - react + sails : Handling Routes

node.js - 上传文件时,heroku 上的 sails 缺少 req.param()

mysql - docker run mysql 图像命令不起作用 [MacBook Pro M1]

php - 如何优化我当前的Mysql查询

linux - ulimits 中的 nproc 和 nofile 是什么?

c# - .NET MongoDB C# 驱动程序不支持转换对象

mysql - 使用 local.js 存储 sails-mysql 密码

Mysql 全文搜索无法按预期工作