fanffis theologiths
fanffis theologiths

Reputation: 33

Express deprecated res.redirect(url, status): Use res.redirect(status, url) instead

I am trying to send a post request and i get this error Express deprecated res.redirect(url, status): Use res.redirect(status, url) instead

app.post("/blogs/:id/likes", isLoggedIn,function(req, res){
//lookup Campground using ID
Blog.findById(req.params.id, function(err, blog){
   if(err){
       console.log(err);
       res.redirect("/blogs");
   } else {
           //add username and id to comment
     var userIndex = blog.likes.indexOf(req.user._id);
if (userIndex > -1) {
blog.likes.splice(userIndex, 1);
} else {
blog.likes.push(req.user);
}
blog.save();


           res.redirect('/blogs/' + blog._id, {blog_id: req.params.id});


   }
});
}); 

Upvotes: 1

Views: 10676

Answers (2)

Hasan Sefa Ozalp
Hasan Sefa Ozalp

Reputation: 7208

Use the res.render method if you want to pass something to your view which accepts (view, locals, callback).

res.render('/blogs/' + blog._id, {blog_id: req.params.id});

res.redirect can only take (status, path).


More info on Express 5x api ⭐️

Upvotes: 2

robertklep
robertklep

Reputation: 203329

Not sure why you're passing an object as second argument to res.redirect(), but it seems to me you want this:

res.redirect('/blogs/' + blog._id);

Upvotes: 2

Related Questions