Difficulty Deleting Cases in Javascript

Asked

Viewed 88 times

1

I am starting to study Javascript and am developing a web application, in which an NGO register and can create cases. I’m using Insomnia to test HTTP requests. However, when trying to delete a case, Insomnia loads the request and has no return, and in the VS Code terminal, I have the following message:

(node:9328) UnhandledPromiseRejectionWarning: ReferenceError: ong is not defined
    at delete (C:\Semana OmniStack\www\semanaomnistack11\aulas\backend\src\controllers\IncidentController.js:50:31)
(node:9328) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an 
async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9328) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.*

PS: all other requests work except this one. The delete method is as follows::

 async delete(request, response) {
     const { id } = request.params;
     const ong_id = request.headers.authorization;

     const incident = await connection('incidents')
         .where('id', id)
         .select('ong_id')
         .first();

     if (incident.ong_id != ong.id) {
         return response.status(401).json({ error: 'Operation not permitted.' });
     }

     await connection('incidents').where('id', id).delete();

     return response.status(204).send();
}
  • Would instead of ong.id you should have ong_id here if (incident.ong_id != ong.id) {?

2 answers

1


In the error it is pointed out that "Ong is not defined" (the variable Ong is not defined). Note that in your code you try to compare incident.ong_id with ong.id, but at no time do you obtain and/or instance Ong.
I believe it was a typo since you get ong_id.
So theoretically it would just replace:

incident.ong_id != ong.id  

for:

incident.ong_id != ong_id  
  • You’re right! Lack of attention, thank you!!

0

Hello, just replace this section:

if (incident.ong_id != ong.id) {
     return response.status(401).json({ error: 'Operation not permitted.' });
 }

For this:

if(incidents.ong_id != ong_id) {
            return response.status(401).json({ error: 'Operation not permitted.'});
        }

It was probably a typo in the word incident, which the correct ones would be incidents (plural).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.