【问题标题】:How to refere same schema as a type in moonsoose [duplicate]如何将相同的模式引用为猫鼬中的类型[重复]
【发布时间】:2020-02-28 05:51:36
【问题描述】:

我有一个评论列表对象,其中存储了 cmets,并且任何回复该评论的人都会将其存储在子项中

{
    "_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};

在这里我试图给孩子类型作为相同的 ChildrenSchema 但它不起作用它正在抛出 { CastError: Cast to embedded failed for value "{ comment: \'hi darling\',\n children: [],\n 用户:\n { _id: \'5dbb81c8c597533bf4c38e75\',\n 用户名: \'arunkavale\',\n 头像:\n \'https://s3.amazonaws.com/uifaces/faces/twitter/johnsmithagency/128.jpg\' } }" at path "children 错误.. 我不知道该怎么做。请帮助我

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    修改Subdocuments数组的定义方式如下,试一试

       children:[ChildrenSchema]
    

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 2018-10-09
      • 2016-01-27
      • 2015-12-18
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      相关资源
      最近更新 更多