猫鼬筑巢

时间:2017-11-09 13:10:25

标签: json mongoose

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

{
    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.

我应该尝试使用子文档还是我缺少语法方面的东西?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试创建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了解更多详情