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 Connecting the API and Database

mulgey
mulgey
9,899 Points

The "update" Method

We had the update method in models.js as following:

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

Then we had this "change the specific question" solution in router.js as:

router.put('/:qID/answers/:aID', (req, res) => {
    req.answer.update(req.body, function(err, results) {
        if (err) return next(err);
        res.json(results);
    });

How should i connect / pair these two in my mindset?

In update method: function(updates, callback) matches with (req.body, function (err, results)?

Or it works in a different way?

Thanks!

1 Answer

Your thinking is correct! The update object is req.body and the callback function is the "function(err, results){...}" function.