0
 app.put('/app/modify/:_id', function(req, res) {
        Collection.findById(req.params._id, function (err, blog) {
            if (err) res.send(err);

            if (req.body.text1) blog.text1 = req.body.text1;
            if (req.body.text2) blog.text2 = req.body.text2;
            if (req.body.text3) blog.text3 = req.body.text3;
            if (req.body.text4) blog.text4 = req.body.text4;
            if (req.body.text5) blog.text5 = req.body.text5;

            Collection.save( function (err) {
                if (err) send (err);
                res.json({message: 'Blog Post Updated!'});
            });
        });
    });

Taken help from this - PUT and DELETE - RESTful API Express MongoDB Mongoose

But getting error- http://localhost:8080/app/modify/59203c7d9532c34903000002 net::ERR_EMPTY_RESPONSE and Node server stops with an error 'Collection.save is not a function'.

1
  • You mean blog.save(function(err) { .... It's an instance method. IMHO It should not be used at all, but that's an entirely different subject. Commented Jun 1, 2017 at 6:14

1 Answer 1

0

Try it...

 app.put('/app/modify/:_id', function(req, res) {
            Collection.findById(req.params._id, function (err, blog) {
                if (err) res.send(err);

                if (req.body.text1) blog.text1 = req.body.text1;
                if (req.body.text2) blog.text2 = req.body.text2;
                if (req.body.text3) blog.text3 = req.body.text3;
                if (req.body.text4) blog.text4 = req.body.text4;
                if (req.body.text5) blog.text5 = req.body.text5;

                blog.save( function (err,response) {
                    if (err) res.json(err);
                    res.json({message: 'Blog Post Updated!'});
                });
            });
        });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx, now it is working but data is not getting update.Not showing any error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.