NodeJS - Mongoose,subdoc对象没有方法.id()或.create()

时间:2013-11-06 18:00:08

标签: javascript node.js express mongoose

我正在尝试使用.create()插入子文档,并使用.id()查询所述文档。我一直在关注指南here

我收到错误:对象没有方法'id()'或'create()'

以下是我的代码:

/db/schemas/AnnouncementsSchema.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var postSchema = new Schema({
    title: String,
    dateCreated: String,
    dateEdited: { type: Date, default: Date.now() },
    summary: String,
    body: String
});

var announcementsSchema= new Schema({
    categoryName: String,
    posts: [postSchema]
});

announcementsSchema.methods.Update = function (callback) {
    console.log('Updating object: ' + this);

    this.save(function (err, object) {
        callback(err, object);
    });
}

var announcements = mongoose.model('announcements', announcementsSchema);
var post = mongoose.model('post', postSchema);

module.exports = {
    announcements: announcements,
    post: post
};

/routes/Announcements.js

var mongoose = require('mongoose');
var announcementsSchema = require('../../db/schemas/AnnouncementsSchema.js');

exports.InsertPost = function (req, res) {

    var announcements = announcementsSchema.announcements;
    var post =  announcementsSchema.post;

    var categoryToEdit = req.body.categoryToEdit;
    var newPost = req.body.newPost;

    announcements.GetById(categoryToEdit._id, function (err, announcment) {

        var postToAdd = new post(newPost);

        announcment.posts.create(postToAdd);

        announcment.Update(function (err, object) {
            res.send({ err: err, data: object});
        });
    });

}

我将.save方法包装好,以便在需要时添加额外的功能。当它调用.create()时我崩溃了。如果我也试图删除帖子,情况也是如此。这是代码:

exports.DeletePost = function (req, res) {

    var announcements = announcementsSchema.announcements;

    var categoryId = req.body.categoryId;
    var postId = req.body.postId;

    announcements.findById(categoryId, function (err, object) {

        var postToDelete = object.posts.id(postId);

        console.log(postToDelete);

        res.end();
    });

}

无论哪种方式,他们都崩溃和谷歌相当苗条的答案。大多数人已经给出了一些不同的方式来展示模式和模型,我上面提到的几乎是他们建议的总和。有任何想法吗?谢谢!

2 个答案:

答案 0 :(得分:3)

我在玩弄这个永远之后找到了答案。这很简单。声明架构的顺序很重要。所以我必须在声明父模式之前声明我的子文档模式。如:

var announcementsSchema= new Schema({
    categoryName: String,
    posts: [postSchema]
});

var postSchema = new Schema({
    title: String,
    dateCreated: String,
    dateEdited: { type: Date, default: Date.now() },
    summary: String,
    body: String
});

这很有效!我现在能够保持所有其余代码相同,但现在方法存在!

答案 1 :(得分:0)

即使我最近选择了Express.js,我也会尽力提供有用的答案。希望它有所帮助!

如果您要访问特定帖子的ID,则会将其存储在._id属性中,而不是id。此外,我相信您首先必须循环object.posts,因为它是一系列帖子:

exports.DeletePost = function (req, res) {

    var announcements = announcementsSchema.announcements;

    var announcementId = req.body.announcementId;
    var postId = req.body.postId; // This is the _id property of an element contained within the posts array
    var postToDelete = {};

    announcements.findById(announcementId, function (err, object) {

        // Get the posts array
        var posts = object.posts;

        // Loop over it until you find the post with the id that you stored in postId
        for (var i = 0; i < posts.length; i ++) {
           if (posts[i]._id == postId) {
               postToDelete = posts[i];
           }
        }

        console.log(postToDelete);

        res.end();
    });

}

对于您的InsertPost函数,您还应该考虑posts是一个数组的事实。因此,您可以简单push将新帖子放入该数组中并相应地保存:

exports.InsertPost = function (req, res) {

    var announcements = announcementsSchema.announcements;
    var post =  announcementsSchema.post;

    var announcementId = req.body.announcementId; 

    var newPost = req.body.newPost;

    announcements.findById(announcementId, function (err, announcment) {

        var postToAdd = new post(newPost);

        announcment.posts.push(postToAdd);

        announcment.save(function(err) {
           if (err) {
               res.send(500, { error: err.stack });
           }

           console.log('Success');

           res.end();
        });
    });

}