Question: NodeJS Update API - Internal Server Error

Question

NodeJS Update API - Internal Server Error

Answers 2
Added at 2016-12-28 04:12
Tags
Question

It's about time I call in the big guns for this as I can't seem to figure it out.
I have a simple CRUD API in Node. I'm using EJS on the front-end. Essentially, I've got a selectAllRecords view where I display a table of all the records. I have a button next to each record to edit the record. When the button is clicked, it redirects to an editrecord.ejs page, hits the API for a single record where each line is displayed as a value in an input box. From there, I have an onclick method with an XMLHttpRequest making a put request to update the database. However, I'm getting an error - 500 (Internal Server Error) - I'm sure it's something fairly simple I'm missing, but I can't seem to figure it out.

Any help is greatly appreciated! Code below:

First on my view:

<script type="text/javascript">
function someFunc() {
    var id = <%= id %>;
    var url = '/api/edit/' + candID;
    console.log('url ' + url);
    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;

    var data = {
      name: name,
      email: email,
    }
    var json = JSON.stringify(data);
    console.log('json ' + json);
    var xhr = new XMLHttpRequest();
    xhr.open("PUT", url, true);
    xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
    xhr.send(json);
};

and in my queries.js file:

function updateCandidate(req, res, next) {
    var candID = parseInt(req.params.id);
    console.log('hit update');
    console.log('name ' + req.body.name);
    db.none('update cands set name=$1, email=$2 where id=$3',
    [req.body.name, req.body.email, candID])
    .then(function () {
      var candID = candID
      var name = data.name;
      var email = data.email;
      res.render("edited", {"candID":candID, "name":name, "email":email});
    })
    .catch(function (err) {
        return next(err);
    });
}

A potentially important note, when I hit the update button and execute the someFunc() function, the dev tool logs show a PUT request to 'api/edit/50' (or whatever ID) and '500 (Internal Server Error)' -- If i hard reload the 'getAllRecords' view, the updates are reflected so it's an issue with the render or redirect (I've tried both)

EDIT

As suggested, I removed the render from the updateCandidate method, but I still get a 500 Internal Server Error. the devtools show me the PUT request is hitting the right URL so i'm really not sure why this isn't functioning correctly. Updated code below...

function updateCandidate(req, res, next) {
var candID = parseInt(req.params.id);
db.none('update cands set name=$1, email=$2, client=$3, jobtitle=$4, question1=$5, question2=$6, question3=$7 where id=$8',
[req.body.name, req.body.email, req.body.client,
  req.body.jobtitle, req.body.question1, req.body.question2, req.body.question3, candID])
.then(function (data, err) {
  res.status(200)
    .json({
      status: 'success',
      message: `Edited Candidate`
    });
})
.catch(function (err) {
  return next(err);
});
}
Answers
nr: #1 dodano: 2016-12-28 04:12

Your db query says

db.none('update cands set name=$1, email=$2 where id=$8', [req.body.name, req.body.email]) ...

Shouldn't it be

db.none('update cands set name=$1, email=$2 where id=$8', [req.body.name, req.body.email, candID])
nr: #2 dodano: 2016-12-28 10:12

You are sending an ajax request to update the record. So, you should not try to render a view or redirect user as the response of this request. Instead, you can send back a JSON object with some properties e.g. "status".

Then on client side, you check the returned JSON response and based on "status" parameter ( or any other you chose ), you can either update your data or reload the page using window.reload on client side.

Source Show
◀ Wstecz