如何在mongo中使用.save方法?
How to use .save method in mongo?
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!'});
});
});
});
得到了帮助 - PUT and DELETE - RESTful API Express MongoDB Mongoose
但是出现错误- http://localhost:8080/app/modify/59203c7d9532c34903000002 net::ERR_EMPTY_RESPONSE 并且节点服务器停止并出现错误 'Collection.save is not a function'。
试一试...
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!'});
});
});
});
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!'});
});
});
});
得到了帮助 - PUT and DELETE - RESTful API Express MongoDB Mongoose
但是出现错误- http://localhost:8080/app/modify/59203c7d9532c34903000002 net::ERR_EMPTY_RESPONSE 并且节点服务器停止并出现错误 'Collection.save is not a function'。
试一试...
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!'});
});
});
});