node.js - 风 sails js : How to populate association after *update* to model?

标签 node.js express sails.js waterline

我有以下 Controller 代码,适用于 indexshowcreate 方法,但当我包含 populate 时更新失败 - 这是什么我做错了吗?

  // User List
  index: function(req, res) {
    User.find()
    .populate('profile')
    .exec(function(err, users) {
      if (err) return res.json(err, 400);
      if (!users) return res.json(users, 404);
      res.json(users, 200);
    });
  },

  // Single User
  show: function(req, res) {
    User.findOne({ username: req.param('username') })
    .populate('profile')
    .exec(function(err, user) {
      if (err) return res.json(err, 400);
      if (!user) return res.json(user, 404);
      res.json(user, 200);
    });
  },

  // Create User
  create: function(req, res) {
    User.create(req.body, function(err, user) {
      if (err) return res.json(err, 400);
      Person.create({user: user.id, slug: user.username}, function(err, profile) {
        if (err) return res.json(err, 400);
        User.update(user.id, {profile: profile.id})
        .populate('profile')
        .exec(function(err, user) {
          if (err) return res.json(err, 400);
        });
        user.profile = profile;
        res.json(user, 200);
      });
    });
  },

  // Update User
  update: function(req, res) {
    var username = req.param('username');
    User.update({ username: username }, req.body)
    .populate('profile')
    .exec(function(err, user) {
      if (err) return res.json(err, 400);
      res.json(user, 201);
    });
  },

最佳答案

根据 documentation update 函数接受一个回调,将更新的记录传递给该回调。 文档中的示例:

// For example, to update a user's name,
// .update(query, params to change, callback)
User.update({
  name: 'sally'
},{
  phone: '555-555-5555'
}, function(err, users) {
  // Error handling
  if (err) {
    return console.log(err);
  // Updated users successfully!
  } else {
    console.log("Users updated:", users);
  }
});

应用于您的代码,它看起来像这样:

// Update User
update: function(req, res) {
  var username = req.param('username');
  User.update({ username: username }, req.body)
  .exec(function(err, users) {
    if (err) {
      return res.json(err, 400);
    }

    var user = users.slice(0,1); // Take the first user in the array
    User.findOne(user.id) // You may try something like User._model(user) instead to avoid another roundtrip to the DB.
    .populate('profile')
    .exec(function(err, user) {
      if (err) {
        return res.json(err, 400);
      }
      res.json(user, 201);
    });
  });
}

关于node.js - 风 sails js : How to populate association after *update* to model?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21487976/

相关文章:

node.js - 快速提供静态文件

javascript - 在javascript中导出默认值

node.js - 如何调试 Sailsjs Controller 中的错误(如 "error: A hook (` Controller )加载失败!”

node.js - 在 Sails js 中检查上传的文件扩展名

node.js - Sails.js:如何在 Bootstrap 中使用 i18n

javascript - 手动安装 Node js 与使用包管理器安装

javascript - Webpack + React : Webpack builds from my node_modules folder in correct order in one place, 以及其他地方的错误顺序

node.js - 从 node.js 中的一个文件调用一个函数

javascript - ('eventName' , function(){...}); 上的这个模式 : . 的名称是什么?

docker - 如何通过 localhost 连接两个 docker 容器?