0
我對MEAN堆棧相當陌生,所以也許我做了一件令人難以置信的傻事。貓鼬嵌套模式驗證錯誤
我有幾個既定的模式(BlogEntry,ForumPost),都需要相同的嵌套模式(在這種情況下,評論),所以我已經將評論架構移動到其自己的文件,以避免任何代碼重複。
在BlogEntry的例子只是看,我有我的blog.js模型如下:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var BlogEntry = new Schema({
title: String,
thumbnailUrl: String,
content: String,
comments: [
{
type: Schema.Types.ObjectId,
ref: 'Comment'
}
]
}, {
timestamps: true
});
var Blog = new Schema({
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User'
},
name: {
type: String,
required: true
},
headerImageUrl: String,
description: String,
blogEntries: [BlogEntry]
}, {
timestamps: true
});
module.exports = mongoose.model('Blog', Blog);
和註釋架構包含在comment.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Comment = new Schema({
commentText: {
type: String,
required: true
},
postedBy: {
type: Schema.Types.ObjectId,
ref: 'User'
},
parentComment: {
type: Schema.Types.ObjectId,
ref: 'Comment'
}
}, {
timestamps: true
});
module.exports = mongoose.model('Comment', Comment);
我的路由器處理博客信息包含以下內容:
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Blog = require('../models/blog');
var Verify = require('./verify');
var blogRouter = express.Router();
blogRouter.use(bodyParser.json());
blogRouter.route('/')
.get(function(req, res, next) {
Blog.find(req.query)
.populate('createdBy')
.exec(function(err, blog) {
if(err) return next(err);
res.json(blog);
});
})
.post(Verify.verifyOrdinaryUser, function(req, res, next) {
req.body.createdBy = req.decoded._id;
Blog.create(req.body, function(err, blog) {
if(err) return next(err);
console.log("Blog created");
var id = blog._id;
res.writeHead(200, { 'Content-Type': 'text/plain'});
res.end('Added the blog with id: ' + id);
});
});
module.exports = blogRouter;
當註釋模式處於分離Ë文件,我發佈了以下要求,我得到一個驗證錯誤:
{
"name": "A Cheesemaker's Adventure",
"headerImageUrl": "images/cheeseAdven.png",
"description": "The story of a man's transformation from a miserable, downtrodden software engineer to a bouyant, joy-filled cheesemaker.",
"blogEntries" : [
{
"title": "I like cheese",
"content": "Here is my story...blah blah blah...",
"comments": [
{
"commentText": "Who's is this guy kidding!"
}
]
}
]
}
<!DOCTYPE html><html><head><title></title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Blog validation failed</h1><h2></h2><pre>ValidationError: Blog validation failed
at MongooseError.ValidationError (C:\Development\hcj-express\node_modules\mongoose\lib\error\validation.js:22:11)
at model.Document.invalidate (C:\Development\hcj-express\node_modules\mongoose\lib\document.js:1410:32)
at EmbeddedDocument.invalidate (C:\Development\hcj-express\node_modules\mongoose\lib\types\embedded.js:190:19)
at EmbeddedDocument.Document.set (C:\Development\hcj-express\node_modules\mongoose\lib\document.js:703:10)
at EmbeddedDocument.Document.set (C:\Development\hcj-express\node_modules\mongoose\lib\document.js:548:18)
at EmbeddedDocument.Document (C:\Development\hcj-express\node_modules\mongoose\lib\document.js:67:10)
at new EmbeddedDocument (C:\Development\hcj-express\node_modules\mongoose\lib\types\embedded.js:30:12)
at EmbeddedDocument (C:\Development\hcj-express\node_modules\mongoose\lib\schema\documentarray.js:27:17)
at DocumentArray.cast (C:\Development\hcj-express\node_modules\mongoose\lib\schema\documentarray.js:254:22)
at DocumentArray.SchemaType.applySetters (C:\Development\hcj-express\node_modules\mongoose\lib\schematype.js:628:12)
at model.Document.set (C:\Development\hcj-express\node_modules\mongoose\lib\document.js:695:18)
at model.Document.set (C:\Development\hcj-express\node_modules\mongoose\lib\document.js:548:18)
at model.Document (C:\Development\hcj-express\node_modules\mongoose\lib\document.js:67:10)
at model.Model (C:\Development\hcj-express\node_modules\mongoose\lib\model.js:41:12)
at new model (C:\Development\hcj-express\node_modules\mongoose\lib\model.js:3175:11)
at C:\Development\hcj-express\node_modules\mongoose\lib\model.js:1847:51</pre></body></html>
但是,如果我擺脫comment.js文件,並移動註釋的模式進入blog.js文件,像這樣,我可以發佈與上面列出的完全相同的消息,驗證錯誤消失,並且我成功發佈數據。
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Comment = new Schema({
commentText: {
type: String,
required: true
},
postedBy: {
type: Schema.Types.ObjectId,
ref: 'User'
},
parentComment: {
type: Schema.Types.ObjectId,
ref: 'Comment'
}
}, {
timestamps: true
});
var BlogEntry = new Schema({
title: String,
thumbnailUrl: String,
content: String,
comments: [Comment]
}, {
timestamps: true
});
var Blog = new Schema({
createdBy: {
type: Schema.Types.ObjectId,
ref: 'User'
},
name: {
type: String,
required: true
},
headerImageUrl: String,
description: String,
blogEntries: [BlogEntry]
}, {
timestamps: true
});
module.exports = mongoose.model('Blog', Blog);
Sooo ...我在這裏做錯了什麼?
對您提到的更改進行了更改,並將其部署到服務器,並且仍然收到相同的錯誤。有趣的是,對用戶的嵌套引用工作正常,即使它在不同的文件中,並且在此模式文件中並不明確需要。 –
如果您在代碼中的其他位置使用了用戶(很可能是verifyOrdinaryUser函數)。然後,以前的'require'將它添加到mongoose緩存中。 保留'''require('./ comment.js')'''並且嘗試改變'''評論:[Comment]'''爲 '''評論:[{0類型:Schema.Types。 ObjectId, ref:'Comment' }]''' –
我會同意@Rafael,這是comment.js的第一次使用,所以它應該在require語句中。我嘗試過使用'comments:[Comment]'和'comments:[{type:Schema.Types.ObjectId,ref:'Comment'}]',但在兩種情況下都會得到相同的錯誤。 –