未加星标

Finding a MongoDB document by ObjectId with Mongoose

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I am trying to update a document in MongoDB by finding it by the ObjectId. The work flow is as follows (this is for a blog).

Create a new post in MongoDB by passing a title and body. The ObjectId is automatically created. Go to edit the post. It uses the ObjectId from the URL to grab it from the database and display it in the same new post form, just with the preexisting values. When the submit button is clicked I want to find the document by the ObjectId and update the values in the database with those in the post form.

Step 1 & 2 work fine, but step 3 doesn't seem to be working. It redirects to the page I need it to. But the database has not been updated. It's the same value as it was before.

Here's the relevant code for the update post portion:

app.js app.post "/office/post/:id/update", ensureAuthenticated, routes.updatePost routes/index.js mongoose = require 'mongoose' ObjectId = mongoose.Types.ObjectId Post = require '../models/Post' ... updatePost: function(req, res) { var o_id, the_id; the_id = req.params.id; console.log(the_id); // 510e05114e2fd6ce61000001 o_id = ObjectId.fromString(the_id); console.log(o_id); // 510e05114e2fd6ce61000001 return Post.update({ "_id": ObjectId.fromString(the_id) }, { "title": "CHANGE" }, res.redirect("/office/edit/posts")); }

I'm using Express and Mongoose.

This is also the post model if that helps:

(function() { var Post, Schema, mongoose; mongoose = require('mongoose'); Schema = mongoose.Schema; Post = new Schema({ title: String, subhead: String, body: String, publish_date: { type: Date, "default": Date.now }, mod_date: { type: Date, "default": Date.now } }); module.exports = mongoose.model('Post', Post); }).call(this);

And here's the code for the edit blog post view:

app.js app.get("/office/post/:id/edit", ensureAuthenticated, routes.editPost); routes/index.js editPost: function(req, res) { return Post.findById(req.params.id, function(err, post) { return res.render('edit-post', { post: post, title: post.title }); }); }

Problem courtesy of: David Yeiser

Solution

The problem is how you call update

return Post.update({ "_id": ObjectId.fromString(the_id) }, { "title": "CHANGE" }, res.redirect("/office/edit/posts"));

The last argument will actually redirect the page, whereas update expects a function to be called when the update is complete

You should pass in

return Post.update({ "_id": ObjectId.fromString(the_id) }, { "title": "CHANGE" }, function(err, model) { if (err) // handleerr res.redirect("/office/edit/posts")); });

That way, we only redirect once the model is successfully updated

Solution courtesy of: Benoir

本文数据库(综合)相关术语:系统安全软件

代码区博客精选文章
分页:12
转载请注明
本文标题:Finding a MongoDB document by ObjectId with Mongoose
本站链接:https://www.codesec.net/view/620981.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(79)