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

我试图插入使用.create()子文档,并使用.id()查询所述文档。 我一直在这里遵循指导

我得到的错误:对象没有方法'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方法包装,所以我可以添加额外的function,如果需要的话。 当它调用.create()时,我崩溃了。 如果我也想删除post,情况也是如此。 这是这个代码:

 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(); }); } 

无论哪种方式,他们都崩溃,谷歌是非常苗条的答案。 大多数人已经给出了不同的方式来揭示模式和模式,而我上面的内容几乎是他们所build议的一个总和。 有任何想法吗? 谢谢!

我永远玩弄了这个答案之后find了答案。 这很简单。 您声明模式的顺序很重要。 所以我不得不在声明我的父模式之前声明我的子文档模式。 如:

 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 }); 

这工作! 我现在能够保持所有的代码相同,但现在的方法存在!

即使我最近拿起Express.js,我也会尽我所能提供一个有用的答案。 希望能帮助到你!

如果你想访问一个特定的职位的ID,它存储在._id属性,而不是id 。 此外,我相信你首先必须遍历object.posts因为它是一个post数组:

 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新postpush入该数组并相应地保存它:

 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(); }); }); }