node.js 使用 Mongoose 通过 ObjectId 查找 MongoDB 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14992123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:13:05  来源:igfitidea点击:

Finding a MongoDB document by ObjectId with Mongoose

node.jsmongodbexpressmongooseobjectid

提问by David Yeiser

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).

我正在尝试通过 ObjectId 查找文档来更新 MongoDB 中的文档。工作流程如下(这是一个博客)。

  1. Create a new post in MongoDB by passing a title and body. The ObjectId is automatically created.
  2. 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.
  3. 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.
  1. 通过传递标题和正文在 MongoDB 中创建一个新帖子。ObjectId 是自动创建的。
  2. 去编辑帖子。它使用 URL 中的 ObjectId 从数据库中获取它并以相同的新帖子表单显示它,只是使用预先存在的值。
  3. 单击提交按钮时,我想通过 ObjectId 查找文档,并使用帖子表单中的值更新数据库中的值。

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.

第 1 步和第 2 步工作正常,但第 3 步似乎不起作用。它重定向到我需要它的页面。但是数据库没有更新。它与之前的值相同。

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

这是更新帖子部分的相关代码:

app.js

应用程序.js

app.post "/office/post/:id/update", ensureAuthenticated, routes.updatePost

routes/index.js

路线/ 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.

我正在使用 Express 和 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

应用程序.js

app.get("/office/post/:id/edit", ensureAuthenticated, routes.editPost);

routes/index.js

路线/ 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
    });
  });
}

采纳答案by Bentheitroad

The problem is how you call update

问题是你怎么打电话 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 updateexpects a function to be called when the update is complete

最后一个参数将实际重定向页面,而update期望在更新完成时调用一个函数

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

这样,我们只在模型成功更新后重定向