-1

I've been stuck on this for way too long without being able to find where is my error, I am making an API with node.js through express and all Restful functions work fine for me except the updating which gets stuck on postman "sending request" while the database is updated without problems, I suspect the error is somewhere in the response but I swear I've tried each combination that came to mind to get the response to no avail. Here's my code for this:

In the router

router.put('/:id', hlpJWT.authenticateToken,  ctrProfile.updateInfo);

In the controller

updateInfo = async function (req, res, next) {
  const input = {
     name: req.body.name,
     password: await srvAuth.cryptPass(req.body.password),
     ..., // other input fields
  };
  let user = await srvProfile.updateProfile(input);
  if (user) res.json(user);
  else {
    res.status(404);
    res.json({ error: "user not found" });
  }
};

in the service

updateProfile = function (input) {
  return new Promise((resolve, reject) => {
    query(
      "UPDATE users SET ? WHERE ?",
      [
        {
          name: input.name,
          ... // more input fields
        },
        { user_id: input.id },
      ],
      function (error, result) {
        if (error) {
          return reject(error);
        }
        return resolve(result);
      }
    );
  });
};

Thanks in advance to anyone willing to help

UPDATE: Ultimately I just updated to express v5 and it works fine there, so it definitely was error handling that was causing it, but I never figured out what specifically.

  • Where does `query` come from? Is it [mysql](https://www.npmjs.com/package/mysql) or something else? – Phil Jun 06 '22 at 23:33
  • you are not handling error in your service correctly, https://javascript.info/promise-error-handling – Nonik Jun 06 '22 at 23:45
  • @Nonik see the Express docs for [error handling](https://expressjs.com/en/guide/error-handling.html)... _"Starting with Express 5, route handlers and middleware that return a Promise will call `next(value)` automatically when they reject or throw an error"_. OP **is** handling errors correctly – Phil Jun 06 '22 at 23:48
  • Are you using Express v4 or v5? – Phil Jun 07 '22 at 00:35
  • I am indeed using express v4 and mysql, sorry for not mentioning that in the post – HelplessStudent88 Jun 07 '22 at 09:24

1 Answers1

1

It sounds like you have an unhandled error on this request which prevents a response from getting sent to your client, in this case, Postman. It could be either the password creation or user update that's triggering this.

If you wrap your request like this, I'm sure your client will get a response:

try {
  const password = await srvAuth.cryptPass(req.body.password);
  const input = {
    ..., // input fields
    password,
  }
  let user = await srvProfile.updateProfile(input);

  if (user) {
    res.json(user);
  } else {
    res.status(404);
    res.json({error: 'user not found'});
  } 
} catch (e) {
  res.status(500).json({error: e, message: 'internal server error'});
}
         
tscafie
  • 61
  • 8
  • Express' [default error handling](https://expressjs.com/en/guide/error-handling.html) already does this – Phil Jun 06 '22 at 23:42
  • Your resource states "You must catch errors that occur in asynchronous code invoked by route handlers or middleware and pass them to Express for processing." Default error handling is only for synchronous code. – tscafie Jun 07 '22 at 00:11
  • Depends if OP is using Express v5 or v4... _"Starting with Express 5, route handlers and middleware that return a Promise will call next(value) automatically when they reject or throw an error"_ – Phil Jun 07 '22 at 00:26
  • This service isn't returning a promise like in the doc, so this response just hangs. Express version and implicit behavior doesn't matter since OP's code fails to handle the asynchronous error states. Further, in the asynchronous example in @Phil's resource it states: " If the try...catch block were omitted, Express would not catch the error since it is not part of the synchronous handler code." – tscafie Jun 07 '22 at 00:49
  • _"This service isn't returning a promise"_... it literally has `return new Promise()`. Also, OP's controller is an `async` function which means it also returns a promise – Phil Jun 07 '22 at 00:58
  • You're right on this point. – tscafie Jun 07 '22 at 01:28
  • 1
    FYI I think you're right with this answer and OP is probably using Express v4. – Phil Jun 07 '22 at 01:30
  • Yup I am using v4, (I know that it cannot be password creation that's causing the error as registering new users works) I tried putting try-catch on both the controller and the service and again no response, this is very weird to me as an inexperienced person. – HelplessStudent88 Jun 07 '22 at 11:48