Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a REST API With Express Completing and Testing the API Test Routes with Postman

Christos Peramatzis
Christos Peramatzis
16,428 Points

PUT not working

The Update method on PUT is not working. Specifically, I get an "undefined is not a function" error message.

In the models.js file, i have the following

AnswerSchema.method("update", function(updates, callback) {
  Object.assign(this, updates, {updatedAt: new Date()});
  this.parent().save(callback);
});

And in the routes.js

router.put("/:questionId/answers/:answerId", function(req, res) {
  req.answer.update(req.body, function(err, result) {
    if(err) {
      return next(err);
    }
    res.json(result);
  });
});

Could anyone help me figure out the error?

Can you post the whole file

3 Answers

Pedro Pólvora
Pedro Pólvora
14,086 Points

I got the same problem, I narrowed it down to a TypeError: undefined is not a function which basically happens because Object.assign is from ECMA6 which is not supported by 'older' node versions..If you update node (4.4.7) it should work fine.

You can check the version of node by node --version. Also, it might be useful to use nvm to manage node versions.

As a side note, if you have homebrew installed, you can upgrade node using the following command:

brew upgrade node
Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,425 Points

Hello there. This is for anyone else who might be having the same issue as above. I just encountered it myself until I realized that I did not have the "next" parameter included in the callback function of the router.put path like so:

router.put("/:qID/answers/:aID", function(req, res, next){
    req.answer.update(req.body, function(err, result){
        if(err)  return next(err);

    res.json(result);

    });

});

Also, make sure to include both the qID and the aID in the url when typing in Postman with the PUT method selected:

localhost:3000/questions/589e34f5d6ce44315bf7fb96/answers/589e367fd6ce44315bf7fb97

Hopefully this helps anyone else who make get stuck at this point.

Cheers:-)

jbiscornet
jbiscornet
22,547 Points

I can't get put to work, I have node v. 8.5 and I included next in the parameters but none of that works for me. It seems to work in the video for Andrew. Everything else works as it should except for this, does anyone else get this to work?