简体   繁体   中英

Sails Js Destroy record

I want to remove a record from my SQL database. And it's strenge because, when I try to find the record, I get it with success. But When I try to remove it, I have an error.

Service.find({id:serviceId}).exec(function(err, srv){

  if(err){res.json({err:err})}

  // some script to verify if the user can remove this record 
  // ....

  if(canRemove){

    Service.destroy({id: serviceId}).exec(function(err){
      if(err){res.json({err:err})}
      res.json('record removed');
    })

  }

})

Did I miss something?

EDIT: the error: 在此处输入图片说明

Code seems to be crashing at res.json in if(err) block.

Change this code:

Service.destroy({id: serviceId}).exec(function(err){
  if(err){res.json({err:err})}
  res.json('record removed');
})

to this:

Service.destroy({id: serviceId}).exec(function(err){
  if(err){
    // sails.log.error('Error', err);
    return res.serverError('Something went wrong');
  }
  res.json('record removed');
})

I have finaly found the solution.

Because when I changed the "return res.serverError('Something went wrong')" by "return res.serverError(err)" I had this message :

Error (E_UNKNOWN) :: Encountered an unexpected error
: ER_BAD_FIELD_ERROR: Champ 'model.undefined' unknown in order clause
at ....

So I make some research and edit my Service Model by adding an primary key as @sgress454 said in this post: Cannot update attributes in Sails.js model instance and then I be able to remove the record perfectly.

Thanks for helping.

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