Node.js postman continually sends requests indefinitely without timing out or encountering errors, successfully updating the database on a PUT request

I've been struggling with this issue for quite some time now. I am developing an API using node.js through express, and all Restful functions are working fine for me except for the update function. When trying to update data, Postman gets stuck on "sending request" while the database is successfully updated. I suspect the problem lies in the response handling, as I have tried numerous combinations without success. Here's a snippet of my code:

Within the router

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

Within 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" });
  }
};

Within 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);
      }
    );
  });
};

Thank you in advance to anyone who can offer assistance.

UPDATE: I eventually updated to express v5 and it resolved the issue, indicating that it was likely related to error handling. However, I was unable to pinpoint the exact cause.

Answer №1

It appears that an unhandled error is causing a delay in sending a response to your client, specifically Postman. The issue may be related to either the password creation process or user update.

To ensure your client receives a response, you can structure your request like this:

try {
  const password = await srvAuth.cryptPass(req.body.password);
  const input = {
    ..., // include relevant 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'});
}
         

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pg-promise: The Correct Method for Retrieving Query Results

I am looking to verify if a specific username is already taken using pg-promise. The query I'm using is as follows: this.db.one('SELECT EXISTS(SELECT 1 FROM users WHERE username = $1)', username); I want to create a function that will ret ...

What is the best way to create a list from a matrix using JavaScript?

I have an array structured as follows: const input_array= [ ["red", "green"], ["small", "medium"], ["x", "y", "z"] //... can have any number of rows added dynamically ...

Modifying the position of image fragments on the background

Currently, I am attempting to create a puzzle using a background image with numbered pieces that will eventually be movable through javascript. However, my progress has hit a snag as I am struggling to position the pieces correctly on the image. The HTML c ...

The requested resource does not have the 'Access-Control-Allow-Origin' header, resulting in a 401 error

I've been working on integrating spell check functionality using the Bing Spell Check API, but I've run into a 401 error. The specific error message is: The requested resource does not have an 'Access-Control-Allow-Origin' header. This ...

Using JavaScript in Wordpress to specifically select and follow the user that was just clicked on

I have implemented the "User Follow System" plugin on my website. I need help in modifying the JavaScript code to select only one user when the current user clicks on the follow button. Currently, I am displaying a list of followers and following users us ...

Decoding Japanese file names using JavaScript in Visual Studio Code

Currently, I am on MacOS 12 using VS Code. An issue arises when I choose a file with a Japanese filename and try to output it to the console: File.openDialog(); $.writeln(file.name); Unfortunately, when this happens, the Japanese characters disappear and ...

What is the best way to combine Node.js MySQL tables into a JSON format and transfer the data to the client?

I'm looking for a way to retrieve MySQL related tables as JSON from NodeJS and send it back to the client. For example, let's say I have a 'students' table and a 'studentCountry' table. The 'students' table has a fie ...

Error: The number of parameters provided does not match the expected count

Encountering an issue in both firefox and google chrome : Sys.ParameterCountException: Parameter count mismatch. The javascript method being called via onclick is as follows: <script type="text/javascript"> var confirmSubmited = false; ...

Utilizing Vanilla JavaScript to retrieve information from a HTML table

I am currently running my website on XAMPP and I have a database connection set up that I can use. In my database, I have three drop-down menus labeled Translations, Books, and Chapters where the data for each is stored. My goal is to utilize Vanilla JS ...

Omit or disregard variable when input field is left blank

Currently, I am working on generating a URL for an API based on user input from a query form using JavaScript. The text field input is stored in a variable and then combined to create the final URL. However, not all input fields will have data entered ever ...

How can an array of objects be sent as a query string to the endpoint URL in React?

I'm currently developing a react application and facing the challenge of dynamically constructing and appending query strings to URLs. This is necessary because I have a shared base endpoint for all links, but each link may require different parameter ...

Problems arising from using the "extends" keyword in express.js

Hey there, I'm currently facing a situation with Express where my templates aren't working correctly with the extends and block keywords. The index template is functioning fine but when I try to add a second template, the 'extends' keyw ...

One effective method for transferring a variable between Express routes

Just getting started with coding a few months back, so please bear with me. Is there a way to access a variable from the placeOrder route in the match route? //Get Price// const placeOrder = (req, res) => { var symbol = req.query.symbolID.split(" ...

What is the process for configuring static file serving in Express with a custom starting route?

In my situation, I am attempting to configure the directory .../whatever/stuff to be served statically and referenced as http://example.com/mystuff. To achieve this, I have tried implementing the following code: app.configure(function() { app.use(&apo ...

Encountering ERR_HTTP_HEADERS_SENT error in Next JS following a failed GraphQL mutation

I've been working with Next JS API routes and encountered an issue while calling a GraphQL mutation using Apollo in one of those routes. Here's an example code snippet: // pages/api/foo.js import { initializeApollo } from '~/server/apollo&a ...

A guide on aligning an object with the cursor position

In a 'open world' game setting, I have a simple sword image drawn pointing up, where the player can freely move around the world to all coordinates. My goal is to make the sword point towards the mouse cursor. One of the challenges I'm faci ...

Conceal and reveal text using JavaScript

I am attempting to create a function where text appears and disappears when clicking on the '+' sign, but I am facing difficulties in getting it to work. Additionally, when I apply this function to other sections, the original one stops working a ...

Store the output of a MySQL query as a variable in JavaScript code

As I work on developing a discord bot, one area that I am focusing on involves implementing a database for certain functions. My current challenge revolves around creating a command that retrieves the names of all tables stored in the database. While I hav ...

Error: react/js encountered an unexpected token

While attempting to execute my project, I encountered an error in the console related to a function within the code. The exact error message reads as follows: "63:25 error Parsing error: Unexpected token, expected (function toggleDrawer = (open) => () ...

Having trouble fitting the network into the bootstrap panel with VIS JS

I have been experimenting with vis.JS to visualize a network graph using some data. However, when I display the network within a bootstrap panel, it appears very small and unreadable. Even zooming in causes loss of perspective on the data. https://i.sstat ...