【问题标题】:Sending JSON obj fields in Insomnia在 Insomnia 中发送 JSON obj 字段
【发布时间】:2018-07-16 17:24:53
【问题描述】:

我正在尝试在我拥有的一个字段中发送一个 JSON 对象,以便将其存储在 MongoDB 中(使用 Mongoose 模式)。现在我面临一个服务器端的 JSON 解析错误。

这就是我要发送的内容

{
"text":"N1y",
"localId":"123",
"type": "media",
"attachments": {
url:"Test.jpg",
publicId:"123"
}
}

我的 Moongose 架构

const messageSchema = mongoose.Schema({
  createdAt: {
    type: String,
    default: new Date()
  },
  chatId: {
    type: String,
    required: true
  },
  localId: {
    type: String,
    required: true
  },
  text: {
    type: String,
    required: true
  },
  authorId: {
    type: String,
    required: true
  },
  type: {
    type: String,
    default: 'text'
  },
  attachments: {},
  style: {
    type: String,
    default: 'normal'
  }

将 Node.JS 与 Express 结合使用。

【问题讨论】:

    标签: node.js mongodb rest express


    【解决方案1】:

    在架构更新附件中输入不正确的 json 对象和请求对象。

    { “文本”:“N1y”, "localId": "123", “类型”:“媒体”, “附件”:{ "url": "Test.jpg", “publicId”:“123” } }

    架构

    attachments: {
      type : Object
    },
    

    attachments: {
        url: {
            type: String
        },
        publicId: {
            type: String
        },
    }
    

    【讨论】:

    • 但是如果我有一组 url 和公共 ID,我该怎么做呢?简单地? "url": ["Test.jpg", "Test2.png"], "publicId": ["123","525"] }
    • 您可以在附件中尝试url: [String]
    猜你喜欢
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2016-11-03
    • 2015-12-16
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多