node.js - NodeJS - Mongoose(对象没有方法 "id")

标签 node.js mongoose

当我在我的应用程序中尝试此代码时:

app/models/Post.coffee

mongoose      = require "mongoose"
CommentModel  = require "./Comment"
Comment       = CommentModel.Schema
Schema        = mongoose.Schema

Post = new Schema {
    title: String,
    slug: {type: String, index: { unique: true, dropDubs: true }},
    content: String,
    author: String,
    tags: [String],
    comments: [Comment],
    created: { type: Date, default: Date.now }
}

Post.statics.findBySlug = (slug, cb) ->
    this.model("Post").findOne({ slug: slug }, cb)

PostModel = mongoose.model "Post", Post

module.exports = PostModel

app/models/Comment.coffee

mongoose  = require("mongoose")
Schema    = mongoose.Schema

Comment = new Schema {
    author: String,
    content: String,
    approved: Boolean,
    created: { type: Date, default: Date.now }
}


CommentModel = mongoose.model "Comment", Comment

module.exports = CommentModel

app/controllers/PostsController.coffee(只有一种方法)

commentDestroy: (req, res, next) ->
    Post.findBySlug req.params.slug, (err, doc) ->
        if (err)
            return next err

        if doc == null
            res.send 404
            return

        doc.comments.id(req.params.comment).remove()
        doc.save (err) ->
            if err
                next err

            res.json doc

以错误结束:

TypeError: Object [object Object],[object Object],[object Object],[object Object],[object Object] has no method 'id'
    at Promise.PostsController.commentDestroy (/home/r41ngoloss/Projects/www/my-express/app/controllers/PostsController.js:88:22)
    at Promise.addBack (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/promise.js:128:8)
    at Promise.EventEmitter.emit (events.js:96:17)
    at Promise.emit (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/promise.js:66:38)
    at Promise.complete (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/promise.js:77:20)
    at Query.findOne (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/query.js:1533:15)
    at model.Document.init (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/document.js:229:11)
    at model.init (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/model.js:192:36)
    at Query.findOne (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/query.js:1531:12)
    at exports.tick (/home/r41ngoloss/Projects/www/my-express/node_modules/mongoose/lib/utils.js:408:16)

我已经尝试为我的问题找到解决方案,但我发现只是 this我认为,我的架构顺序正确(按 require)。

感谢您的每一个回答。

最佳答案

您收到该错误的原因是您没有在 Post.coffee 文件中正确获取 Comment 架构。当我执行您所做的操作时,Comment 变量是未定义。将 Post.coffee 文件的顶部修改为:

mongoose = require "mongoose"
Comment  = mongoose.model('Comment').schema
Schema   = mongoose.Schema

现在,Comment 变量是 Comment 模型的架构。

关于node.js - NodeJS - Mongoose(对象没有方法 "id"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15173431/

相关文章:

node.js - Mongoose 对象 ID 为空

mongoose - 使用 mongoose 全文搜索和 KeystoneJS 分页

javascript - Node.js 作为长轮询客户端

node.js - nodejs : Access variables in routes

javascript - CouchDB 循环唯一的 `_id` 和通过 JavaScript 的 Console.log

node.js - 如何在 mongoose 5.x.x 中使用 arrayFilters?

javascript - 异步等待众多之一

node.js - 如何用Node.js的fs模块和express实现文件下载

node.js - 如何创建回调后保存对象数组?

javascript - 更新引用另一个模型的属性而不重复