我正在嘗試構建包含以下內容的評論模型:Reply和CommentThread。 CommentThread包含Reply,並且Reply可以遞歸自己。模式數組路徑的值無效
/models/comment.js:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var replySchema = new Schema({
username: String,
timestamp: { type: Date, default: Date.now },
body: String,
replies: [replySchema]
}, {_id: true});
var commentThreadSchema = new Schema({
title: String,
replies: [replySchema]
});
var Reply = mongoose.model('Reply', replySchema);
var CommentThread = mongoose.model('CommentThread', commentThreadSchema);
module.exports = {
Reply: Reply,
CommentThread: CommentThread
};
我的錯誤信息是:對於架構陣列路徑無效值 '回覆'。不能replySchema使用自己作爲值類型?或者其他一些原因?
c:\Users\jacki_000\projects\invictusblog\node_modules\mongoose\lib\schema.js:297
throw new TypeError('Invalid value for schema Array path `'+ prefix + ke
^
TypeError: Invalid value for schema Array path `replies`
at Schema.add (c:\Users\jacki_000\projects\invictusblog\node_modules\mongoos
e\lib\schema.js:297:13)
at new Schema (c:\Users\jacki_000\projects\invictusblog\node_modules\mongoos
e\lib\schema.js:87:10)
at Object.<anonymous> (c:\Users\jacki_000\projects\invictusblog\models\comme
nt.js:4:19)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (c:\Users\jacki_000\projects\invictusblog\services\com
ment-service.js:1:83)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
謝謝。有用。是因爲在使用replySchema之前,應該定義它? – invictus
謝謝,從舊版本的貓鼬中遷移時遇到同樣的問題。問題是如何定義模式。 '.add'很好地工作。 – jmunsch
這解決了這個問題,但我認爲它完全愚蠢! – dd619