本文介绍了架构数组路径的值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建包含以下内容的评论模型:Reply和CommentThread. CommentThread包含Reply,Reply可以递归.

I am trying to build comment model contains: Reply and CommentThread. CommentThread contains Reply, and Reply can recurse itself.

/models/comment.js:

/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将自身用作值类型?还是其他原因?

My error message is : Invalid value for schema Array path 'replies'. Can't replySchema use itself as value type ? Or some other reasons?

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)

推荐答案

https://searchcode.com/codesearch/view/6134527/

请参见上面的示例,您需要做类似的事情

see the example above, you need to do something like

var replySchema = new Schema();
replyschema.add({
  username: String,
  timestamp: { type: Date, default: Date.now },
  body: String,
  replies: [replySchema]
});

这篇关于架构数组路径的值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:02