【问题标题】:Schema referencing stores the whole value instead of ObjectId alone模式引用存储整个值而不是单独存储 ObjectId
【发布时间】:2018-07-05 16:30:20
【问题描述】:

最近,我学习了一门名为Web 开发人员课程 的课程。 其中最终项目基于 Camps。 项目中引用了comment数据库和campground数据库,即cmetsObjectIds发布在 campground 中的内容存储在 array 中。这就是实际发生的事情。

但在我的情况下,确切的情况发生了变化。当我尝试添加 新评论 时,实际发生的是 total object 存储在 cmets 数组,而不是评论的ObjectId。 我几乎已经通过 Stackoverflow 为我的问题寻求解决方案,但失败了。

我只想将 ObjectId 存储在 cmets 数组 中,而不是存储整个 Object,这给我带来了更新和删除的问题一条评论。当我删除或更新评论时,该操作确实发生在 Comments 数据库中,但不会反映在 Campgrounds 数据库中。请帮我解决这个问题。如果有人参加相同的课程,如果您已经经历过类似的事情,请给我解决方案。 Schema 如下所示

露营地架构:

var mongoose = require("mongoose");

var campgroundSchema = mongoose.Schema({
    campGroundName: String,
    campGroundImage: String,
    description: String,
    comments: [
        {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Comment"
        }    
    ],
    addedBy: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
        },
        username: String
    }
});

module.exports = mongoose.model("Campground", campgroundSchema);

评论架构:

var mongoose = require("mongoose");

var commentSchema = mongoose.Schema({
    text: String,
    author: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
        },
        username: String
    }
});

module.exports = mongoose.model("Comment", commentSchema);

发表评论请求:

router.post("/", middleware.isLoggedIn, function(req, res) {
    Comment.create(req.body.comment, function(err, createdComment) {
          if(err) {
              console.log(err);
          } else {
              createdComment.author.id = req.user._id;
              createdComment.author.username = req.user.username;
              createdComment.save();                  Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
              foundCampground.comments.push(createdComment);
              foundCampground.save();
              req.flash("success" , "Comment created successfully");
              res.redirect("/index/" + req.params.id); 
             });
          }        
    });
});

完整的源代码如下,

https://1drv.ms/u/s!AmISAco3PGaPhQl_Riu8nroCom5h

请帮我解决这个问题!

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query mongoose-schema


    【解决方案1】:

    该版本似乎有错误。 当我更新它时,问题得到了解决。固定版本是5.0.3

    【讨论】:

      【解决方案2】:

      你有这行:

      foundCampground.comments.push(createdComment)
      

      这是告诉 mongodb 将整个评论存储在数组中。

      应该是这样的:

      foundCampground.comments.push(createdComment._id)
      

      这只会将评论的 id 属性推送到数组中。

      【讨论】:

      • 这给了我在 showCamp 页面中的错误。它不在那里显示评论。 cmets top下是空白的。。我的show camp页面路由是router.get("/:id", function(req, res) { Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground) { if(err) { console.log(err); } else { res.render("campgrounds/showCamp", {campground: foundCampground}); } }); });
      • 您在尝试保存时是否在控制台上遇到任何错误?更重要的是,你看到mongodb中保存的数据了吗?
      • 我在尝试保存时没有收到任何错误。评论在评论集合中得到正确更新,一切正常。但它不会在 showcamp 页面中显示评论:(.. 你能从我上传到 onedrive 的总源代码中检查 showCamp.ejs。
      • 对不起,我应该更明确一点。我的意思是问评论的 id 是否保存在露营地的子文档数组中。
      • 您可以转到gist.github.com 并使用来自您的showcamp 路线和您的showcamp ejs 文件的源创建一个要点吗?只需在此处发布指向要点的链接,我会看看。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 2013-01-29
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      相关资源
      最近更新 更多