Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  41] [ 5]  / answers: 1 / hits: 44169  / 7 Years ago, sun, march 19, 2017, 12:00:00

I'm running a Node.js-server and trying to test this Rest API that I made with Express. It's linked up to MongoDB using Mongoose.



I'm testing the individual routes using Postman and I get an error when trying to send a PUT-request to this route:



// PUT /meetings/:id
// Route for editing a specific meeting
router.put(/:id, function(req, res, next) {
req.meeting.update(req.date, function(err, result) {
if(err) return next(err);
res.json(result);
});
});


The error retrieved is this:



events.js:141
throw er; // Unhandled 'error' event
^

TypeError: next is not a function


I cannot figure out where exactly this is coming from. I'm using the router.params-method to specify how the :id-parameter should be handled like this:



router.param(id, function(req, res, id, next) {
Meeting.findById(id, function(err, meeting) {
if (err) return next(err);
if (!meeting) {
err = new Error(Meeting not found);
err.status = 404;
return next(err);
}
req.meeting = meeting;
return next();
});
});

More From » node.js

 Answers
14

So I figured it out. It was a much smaller error than I thought. I had the parameters to the callback-function in my router.param-method in the wrong sequence. The next-keyword should be where id was. This code fixed the problem:



router.param(id, function(req, res, next, id) {
Meeting.findById(id, function(err, meeting) {
if (err) return next(err);
if (!meeting) {
err = new Error(Meeting not found);
err.status = 404;
return next(err);
}
req.meeting = meeting;
return next();
});
});

[#58485] Thursday, March 16, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raveno

Total Points: 453
Total Questions: 92
Total Answers: 92

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
raveno questions
;