【问题标题】:Mongoose cannot update array: undefined errorMongoose 无法更新数组:未定义的错误
【发布时间】:2018-06-26 07:59:42
【问题描述】:

请看这段代码:

// Update route
router.put("/:comment_id", function (req, res) {
    Campground.findOneAndUpdate(
        {"_id": req.params.id, "comments._id": req.params.comment_id },
        {"$set": {"comments.$.text": req.body.comment.text}},
        function (err, campground) {
            if (err) {
                console.log("campground: " + campground); //logs campground: undefined
                console.log("comment id: " + req.params.comment_id); // logs comment id: 5a5f2ab0a6dccd210c0136de
                console.log("campground id: " + req.params.id); // logs campground id: 5a5e083af62da603900ab0d4
                res.redirect("/campgrounds");
            } else {
                res.redirect("/campgrounds/" + campground._id);
            }
        }
    );
});

ids 指的是这样的 url:

http://localhost:4000/campgrounds/5a5e083af62da603900ab0d4/comments/5a5e0845f62da603900ab0d5/edit

这对我来说很奇怪,为什么我无法联系到campground?这样我就无法更新我需要的文档。请帮助我找到解决方案。

编辑:console.log(err); 提供的错误:

{ CastError: Cast to ObjectId failed for value "secondo me no" at path "comments"
    at new CastError (folder\node_modules\mongoose\lib\error\cast.js:27:11)
    at ObjectId.cast (folder\node_modules\mongoose\lib\schema\objectid.js:158:13)
    at ObjectId.SchemaType.applySetters (folder\node_modules\mongoose\lib\schematype.js:695:12)
    at ObjectId.SchemaType._castForQuery (folder\node_modules\mongoose\lib\schematype.js:1066:15)
    at ObjectId.castForQuery (folder\node_modules\mongoose\lib\schema\objectid.js:198:15)
    at ObjectId.SchemaType.castForQueryWrapper (folder\node_modules\mongoose\lib\schematype.js:1035:15)
    at castUpdateVal (folder\node_modules\mongoose\lib\services\query\castUpdate.js:345:19)
    at walkUpdatePath (folder\node_modules\mongoose\lib\services\query\castUpdate.js:229:22)
    at castUpdate (folder\node_modules\mongoose\lib\services\query\castUpdate.js:72:18)
    at model.Query._castUpdate (folder\node_modules\mongoose\lib\query.js:2991:10)
    at castDoc (folder\node_modules\mongoose\lib\query.js:3017:18)
    at model.Query.Query._findAndModify (folder\node_modules\mongoose\lib\query.js:2292:19)
    at model.Query.Query._findOneAndUpdate (folder\node_modules\mongoose\lib\query.js:2139:8)
    at folder\node_modules\kareem\index.js:276:8
    at folder\node_modules\kareem\index.js:23:7
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
  message: 'Cast to ObjectId failed for value "secondo me no" at path "comments"',
  name: 'CastError',
  stringValue: '"secondo me no"',
  kind: 'ObjectId',
  value: 'secondo me no',
  path: 'comments',
  reason: undefined }

@Younel 这是编辑视图:

<% include ../partials/header %>
<div class="container">
    <div class="row">
        <h1 style="text-align: center;">Edit Comment</h1>
        <div style="width: 30%; margin:25px auto;">
            <form action="/campgrounds/<%= campground_id %>/comments/<%= comment._id %>?_method=PUT" method="POST">
                <div class="form-group">
                    <input class="form-control" type="text" name="comment[text]" value="<%= comment.text %>">
                </div>
                <div class="form-group">
                    <button class="btn btn-lg btn-primary btn-block">Submit!</button>
                </div>
            </form>
            <a href="/campgrounds">Go Back</a>
        </div>
    </div>
</div>
<% include ../partials/footer %>

“secondo me no”是comment[text]

这是一个 github 仓库:https://github.com/ufollettu/YelpCamp.git(运行 v10/app.js)

【问题讨论】:

  • 那么console.log(err) 呢?你知道有一个错误,但你甚至没有记录它。
  • 您确定 req.params.id 已按预期设置?您的程序似乎正确获取了评论 ID,但找不到 Campground,这表明 Campground id 没有正确传递给 mongoose 模型。
  • @Jeremy:我做了一个编辑,请看上面的问题
  • @Sergeon 我认为是的:我在我的应用中使用var commentsRoutes = require("./routes/comments")app.use("/campgrounds/:id/comments", commentsRoutes);
  • 啊,所以不是undefined error。显然是Cast to ObjectId failed。显然它正在通过"secondo me no" 作为_id

标签: javascript arrays mongodb express mongoose


【解决方案1】:

如果我得到your models 正确,comments 不仅仅是一个数组,而是一个embedded documents 的数组,所以更新必须是这样的:

Campground.findById(req.params.id, function(err, doc) {
    if (err || !doc) {
        ...
    }
    const comment = doc.comments.id(req.params.comment_id);
    if (!comment) {
        ...
    }
    comment.text = req.body.comment.text;
    doc.save(function (err) {
        if (err) {
            ...
        }
    })
})

对于参考文件:

router.put("/:comment_id", function (req, res) {
    Comment.findOneAndUpdate(
        {"_id": req.params.comment_id},
        {"$set": {"text": req.body.comment.text}},
        function (err, comment) {
            if (err) {
                ...
            } 
            res.redirect("/campgrounds/" + req.params.id);
        }
    );
}); 

【讨论】:

  • 控制台说TypeError: doc.comments.id is not a function
  • 是的,我的错。 campgroundSchema 中的评论应该是 [commentSchema] - 你在那里评论的东西。
  • 引用文档不能代替嵌入式文档吗?
  • 引用的文档存储在单独的集合中。如果您打算使用它,您将需要在自己的集合中更新comment 文档,甚至无需触摸Campground
猜你喜欢
  • 1970-01-01
  • 2021-01-08
  • 2020-04-27
  • 2017-12-25
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多