Loopback查找然后更新属性或按id删除

时间:2022-11-25 19:29:19

Been trying to find samples usage for some of the static methods for a persistedModel in Loopback.

一直试图在Loopback中找到persistedModel的一些静态方法的样本用法。

https://apidocs.strongloop.com/loopback/#persistedmodel-prototype-updateattribute

https://apidocs.strongloop.com/loopback/#persistedmodel-prototype-updateattribute

it just says:

它只是说:

persistedModel.updateAttributes(data, callback)

But how you I choose the which record I want to update? this is not working for me.

但是你如何选择我要更新的记录呢?这不适合我。

var order = Order.setId('whateverrecordId');
order.updateAttributes({name:'new name'},callback)

Loving loopback.. but their doc, sucks.. :(

爱环回..但他们的文件,糟透了.. :(

2 个解决方案

#1


2  

You can use those on event listener like AfterSave

您可以在AfterSave等事件监听器上使用它们

example:

例:

Model.observe('after save', function(ctx, next) {
   ctx.instance.updateAttribute(fieldname:'new value');
   next();
});

#2


2  

1- What you did was right but i do not advise this method it's used for instance methods and generally to update fields like date for all the collection that you have so you don't need an id for it.

1-你所做的是正确的,但我不建议这种方法用于实例方法,并且通常更新所有集合的日期字段,因此你不需要id。

But you can try to make an array containing data to update containing also the ids and then make a comparison to fill in data for the ids that you have. (in #dosomething)

但您可以尝试创建一个包含要更新的数据的数组,同时包含ID,然后进行比较以填充您拥有的ID的数据。 (在#dosomething中)

order.find().then(function(orders) {

          orders.forEach(function(element) {

            order.setId(element.id); 
            #DoSomething
            order.updateAttribute({new: data}, function(err, instance) {

              console.log(instance);
            })
          });
        })

2- You can use updateAll to update one or many attribute.

2-您可以使用updateAll更新一个或多个属性。

PersistedModel.updateAll([where], data, callback)

PersistedModel.updateAll([where],data,callback)

var Updates = [{id : 1, name: name1}, ...]
Updates.forEach(function(element) {
order.updateAll({id : element.id}, {name :element.name}, function(err, count) {
      if (err) {
        console.error(err);
      }
      console.log(count); // number of data updated
    })
 })

#1


2  

You can use those on event listener like AfterSave

您可以在AfterSave等事件监听器上使用它们

example:

例:

Model.observe('after save', function(ctx, next) {
   ctx.instance.updateAttribute(fieldname:'new value');
   next();
});

#2


2  

1- What you did was right but i do not advise this method it's used for instance methods and generally to update fields like date for all the collection that you have so you don't need an id for it.

1-你所做的是正确的,但我不建议这种方法用于实例方法,并且通常更新所有集合的日期字段,因此你不需要id。

But you can try to make an array containing data to update containing also the ids and then make a comparison to fill in data for the ids that you have. (in #dosomething)

但您可以尝试创建一个包含要更新的数据的数组,同时包含ID,然后进行比较以填充您拥有的ID的数据。 (在#dosomething中)

order.find().then(function(orders) {

          orders.forEach(function(element) {

            order.setId(element.id); 
            #DoSomething
            order.updateAttribute({new: data}, function(err, instance) {

              console.log(instance);
            })
          });
        })

2- You can use updateAll to update one or many attribute.

2-您可以使用updateAll更新一个或多个属性。

PersistedModel.updateAll([where], data, callback)

PersistedModel.updateAll([where],data,callback)

var Updates = [{id : 1, name: name1}, ...]
Updates.forEach(function(element) {
order.updateAll({id : element.id}, {name :element.name}, function(err, count) {
      if (err) {
        console.error(err);
      }
      console.log(count); // number of data updated
    })
 })