简体   繁体   中英

How to change the field delete instead of id in loopback?

I want to the delete a few rows in MySQL table using loopabck. But I don't want to use the id to delete the record. I'm trying it in Angular SDK. My code is:

ModelName.destroyById({ fieldName : myValue })
                          .$promise
                          .then(function() {

                          });

I'd be very thankful for ideas.

Seems like you need to use the destroyAll method, which does what you want, but you need to set up remoting for it first, since it's disabled by default:

  ModelName.remoteMethod('destroyAll', {
    isStatic: true,
    description: 'Delete all matching records',
    accessType: 'WRITE',
    accepts: {arg: 'where', type: 'object', description: 'filter.where object'},
    http: {verb: 'del', path: '/'}
  });

Then after that you regenerate your Angular client and you can use it like this in the client side:

ModelName.destroyAll({ filter: { fieldName : myValue }}).$promise.then(...)

Note that using destroyAll is risky as if you have any mistakes in your code you will end up losing data. Enabling it to client side might be security concern.

Another way to do this is to write your own remote method which uses destroyAll in turn with proper validation of the inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM