skip to Main Content

Scenario

I’ve had a problem for 4 hours, I’m trying to send an http get request while sending the user ID as a parameter. I try a lot of examples found on the net but I still have this error on the backend side.

GET http://localhost:3000/api/users/getusersbyid/?userId=00c1308a-32ad-48a0-8737-d4682b2b504e 500 (Internal Server Error)

Here is my JS function code:

async function getUserById() {
   try {
      await $.ajax({
         type: "GET",
         url: "http://localhost:3000/api/users/getusersbyid",
         data: {
            userId: "00c1308a-32ad-48a0-8737-d4682b2b504e"
         },
         contentType: "application/x-www-form-urlencoded"
      }).done(function(response) {
         console.log(response);
      }).fail(function(err) {
         console.log(err);
      });
   } catch (error) {
      console.log(error);
   }
}

Here is my Backend function code using NodeJs:

getUserById: function(req, res) {
   let userId = req.body.userId;
   models.User.findOne({
      where: {
         id: userId
      },
      include: [{
         model: models.TypeUser,
         attributes: ['code', 'value']
      }]
   }).then(function(data) {
      if (data) {
         res.status(201).json({
            'status': 'success',
            'code': 201,
            'data': data
         });
      } else {
         res.status(404).json({
            'status': 'falled',
            'code': 404,
            'message': 'Unable to find one or more users'
         });
      }
   }).catch(function(err) {
      res.status(500).json({
         'status': 'falled',
         'code': 500,
         'message': 'An internal error has occurred',
         'data': err
      });
   });
}

Here is my Backend Error Message image:

Need your help and suggestions

2

Answers


  1. Chosen as BEST ANSWER

    I just solved the problem after reading the answers from @AbhishekKumawat and from @Pointy. So using the "GET" method, I should do this:

    let userId = req.query.userId;
    

    instead.

    let userId = req.body.userId;
    

  2. It seems something’s going on in your backend. Have you tried using logging, for example after your “let userId = req.body.userId;” line to see if your server is receiving the userId?

    console.log("backend received userId="+userId)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search