【问题标题】:Mongoose nesting猫鼬筑巢
【发布时间】:2017-11-09 23:33:04
【问题描述】:

我正在尝试转换以下伪代码:

{
    app_id: 1,
    help_id: 1234,
    locale: 'en',
    {
        help_title: 'This is the title of the help entry',
        help_description: 'This is the description of the help entry',
        help_content: {[
            { type: 'text', title: '', description: '', style: { ... }},
            { type: 'image', title: '', description: '', url: '', style: { ... }},
            { type: 'image', title: '', description: '', url: '', style: { ... }},
            { type: 'video', title: '', description: '', url: '', style: { ... }},
            { type: 'link', title: '', description: '', url: '', style: { ... }},
            { type: 'svg', title: '', description: '', url: '', style: { ... }},
            { type: 'pdf', title: '', description: '', url: '', style: { ... }},
            { type: 'document', title: '', description: '', url: '', style: { ... }},
        ]}
    },
    locale: 'da',
    { ... }

对于实际代码,这是我目前所拥有的:

var mongoose = require('mongoose')
var Schema = mongoose.Schema

var HelpitemSchema = new Schema({
    app_id: { type: Number },
    help_id: { type: Number },
    locale: { type: String },
    details: { [ 
        {help_title: { type: String }, 
        help_description: {type: String}}
    ] }
})

但我得到了一个:

[eslint]
Parsing error: Unexpected token

   9 |         {help_title: { type: String }, 
  10 |         help_description: {type: String}}
> 11 |     ] }
     |       ^
  12 | })
[js] ':' expected.

我应该尝试使用子文档还是在语法方面遗漏了什么?

任何帮助将不胜感激。

【问题讨论】:

    标签: json mongoose


    【解决方案1】:

    您可以尝试创建一个 contentSchema,然后在 HelpitemSchema 中使用它:

    var contentSchema = new Schema( { 
        type: String, 
        title: String, 
        description: String, 
        style: Mixed 
    });
    
    var HelpitemSchema = new Schema({
        app_id: { type: Number },
        help_id: { type: Number },
        locale: { type: String },
        details: { 
            help_title: { type: String }, 
            help_description: { type: String },
            help_content: [contentSchema]
        }
    });
    

    查看here了解更多详情

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 2017-10-08
      • 2020-04-24
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多