帆水线中的多个模型更新 [英] Multiple model updates in sails waterline

查看:21
本文介绍了帆水线中的多个模型更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sails (0.10.0-rc5) 上构建了一个项目几天,在某些情况下,我需要使用相同的数据一次更新多个条目,所以我做了一些...

I'm building a project on sails (0.10.0-rc5) for a few days and in a few cases i need to update multiple entries at once with the same data so I made something up ...

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

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

});

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

The interesting part is queries.save_each() which I created ...

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

     });

}

}

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

Basically, it's checking each entry and updating it from the new datas with save(). It works fine, but i'm wondering if there's nothing already done in waterline to do so ; I didn't find anything, but i'm quite beginner in sails maybe i missed something !

有什么想法吗?

推荐答案

对于记录,要更新 Sails 中的记录,使用 update 方法:

For the record, to update records in Sails, use the update method:

Model.update(criteria, data).exec(callback);

例如:

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

update 的文档位于 这里.

这篇关于帆水线中的多个模型更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆