gpt4 book ai didi

node.js - 如何引用与 mongoose 中的类型相同的架构

转载 作者:太空宇宙 更新时间:2023-11-03 23:52:57 26 4
gpt4 key购买 nike

我有一个评论列表对象,用于存储评论,无论谁回复该评论,它都会存储在子级中

{
"_id": "5dbc479babc1c22683b73cf3",
"comment": "wow .. this is awsome",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "second child",
"children": [
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "hi darling",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
},
{
"_id": "5dbc481ec3bb512780ebda22",
"comment": "yep",
"children": [],
"user": {
"_id": "5dbb81c8c597533bf4c38e75",
"username": "arunkavale",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg"
},
"updatedDate": "2019-11-01T14:58:38.188Z",
"createdDate": "2019-11-01T14:58:38.188Z"
}
],
"user": {
"_id": "5dbb9683b44bfa2a3dce55bd",
"username": "mayank",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/alxndrustinov/128.jpg"
},
"createdDate": "2019-11-01T14:56:27.580Z",
"updatedDate": "2019-11-01T14:58:38.188Z",
"__v": 0
}

这是我设计的架构

var mongoose = require('mongoose');


let UserSchema = new mongoose.Schema({
username:{
type:String
},
avatar:{
type:String
}
});


var ChildrenSchema = new mongoose.Schema({
"comment":{
type:String
},
parentId:{
type: mongoose.Schema.Types.ObjectId,
},
children:{
type:[ChildrenSchema]
},
user:{
type:UserSchema
}
},{timestamps: { createdAt: 'createdDate', updatedAt: 'updatedDate' }});






let CommentSchema = new mongoose.Schema({
user:{
type:UserSchema,
},
"comment":{
type:String
},
children:{
type:[ChildrenSchema]
}
},{timestamps: { createdAt: 'createdDate', updatedAt: 'updatedDate' }});


var Comment = mongoose.model('Comment', CommentSchema);
module.exports = {Comment};

在这里,我试图为 children 提供相同的ChildrenSchema类型,但它不起作用,它抛出 { CastError: Cast to嵌入失败的值“{ comment:\'hi darling\',\n kids: [],\n 用户:\n { _id:\'5dbb81c8c597533bf4c38e75\',\n 用户名:\'arunkavale\',\n 头像:\n\' https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg\' } }"位于路径“children 错误..我不知道如何做到这一点。请帮助我

最佳答案

修改子文档数组的定义方式如下并尝试

   children:[ChildrenSchema]

关于node.js - 如何引用与 mongoose 中的类型相同的架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58667586/

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