Skip to content Skip to sidebar Skip to footer

Api Resolved Without Sending A Response For /api/users/create, This May Result In Stalled Requests. Nextjs

I created an API endpoint to handle user account creation in my Next.js app, and I am using knex.js to handle my queries, but I still get this error: API resolved without sending

Solution 1:

actually the bcrypt hash function is an async function, it returns a promise to be either resolved with the encrypted data salt or rejected with an Error.

import knex from'../../../knex';

exportdefaultasyncfunctionregiterUser(req, res) {
  if (req.method === 'POST') {
    try {
      const hashed = awaithash(req.body.password, 10);
      awaitknex('users').insert({
        name: req.body.name,
        email: req.body.email,
        role: 'user',
        allowed: true,
        password: hashed,
      });
      res.status(200).end();
    } catch (err) {
      res.status(err).json({});
    }
  } else {
    res.status(405);
    res.end();
  }
}

Solution 2:

In the else block of if statement res.end() must be called.

} else {
  res.status(500)
  res.end()
}

To make API response clearer, consider using 405 status code instead of 500 in this case. 405 means method is not allowed (see here).

Post a Comment for "Api Resolved Without Sending A Response For /api/users/create, This May Result In Stalled Requests. Nextjs"