gpt4 book ai didi

mongoose - 架构数组路径的值无效

转载 作者:行者123 更新时间:2023-12-03 16:44:23 26 4
gpt4 key购买 nike

我正在尝试构建的评论模型包含: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)

最佳答案

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

看上面的例子,你需要做类似的事情

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

关于mongoose - 架构数组路径的值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30856208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com