帆水线中的多个模型更新

Lau*_*ent 0 node.js sails.js waterline

我正在建造一个关于帆(0.10.0-rc5)的项目几天,在少数情况下我需要使用相同的数据一次更新多个条目,所以我做了一些...

Servers.find({owner_id: anonymous_user.id}).exec(function(error, servers) {

  catches.error(error);
  queries.save_each(servers, {owner_id: user.id});

});
Run Code Online (Sandbox Code Playgroud)

有趣的部分是我创建的queries.save_each()...

/**
 * Save each ActiveRecord objects with the desired attributes
 * @param  {object} objects ActiveRecord object (e.g. servers, users)
 * @param  {object} updates datas to update
 * @return {void}
 */
save_each  = function(objects, updates) {

// For each object we will update the wanted datas
for (var n in objects) {

    objects[n] = variables.inject(objects[n], updates);
    objects[n].save(function(error) { 

        catches.error(error);

     });

}

}
Run Code Online (Sandbox Code Playgroud)

基本上,它检查每个条目并使用save()从新数据更新它.它工作正常,但我想知道在水线上是否已经没有做过这样做; 我没有找到任何东西,但我是风帆初学者,也许我错过了什么!

任何的想法 ?

sgr*_*454 7

要记录,要更新Sails中的记录,请使用以下update方法:

Model.update(criteria, data).exec(callback);
Run Code Online (Sandbox Code Playgroud)

例如:

Servers.update({owner_id: anonymous_user.id}, {owner_id: user.id})
       .exec(function(err, updatedServers) {
                 // do something
             });
Run Code Online (Sandbox Code Playgroud)

文档update就在这里.