Sailsjs: как заполнить ассоциацию после *обновления* модели?

У меня есть следующий код контроллера, который работает для методов index, show, create, но обновление завершается с ошибкой, когда я включаю 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);
    });
  },

person rjmoggach    schedule 31.01.2014    source источник
comment
Чего вы пытаетесь достичь с помощью .populate('profile')? Извините, я новичок в парусном спорте и ватерлинии. Всегда ли необходимо звонить .populate()?   -  person Cody    schedule 05.07.2015


Ответы (2)


Согласно документации, функция 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);
    });
  });
}
person Jérémie Parker    schedule 02.02.2014
comment
код не работает... см. мой ответ ниже... отрегулируйте, и я дам вам кредит - person rjmoggach; 06.03.2014

Джереми, ваш ответ по сути правильный, но есть несколько вопросов:

  • пользовательский объект, возвращенный вызовом обновления, а затем нарезанный, не имеет функции populate

  • срез списка users возвращает список вместо желаемого объекта

Личное предпочтение, но я также предпочитаю следующее, в котором вместо обратного вызова используется вызов exec.

  update: function(req, res) {
    var id = req.param('id');
    User
    .update(id, req.params.all())
    .exec(function(err, users) {
      if(err) return res.json(err, 400);
      var user = users[0];
      console.log('ID', user.id);
      User
      .findOne(user.id)
      .populate('profile')
      .exec(function (err, user){
        if (err) return res.json(err, 400);
        res.json(user, 201);
      });
    });
  },
person rjmoggach    schedule 27.02.2014