javascript - 验证器错误 JSON

标签 javascript json node.js

使用 Postman 时,当我尝试发布问题 id 的答案时,它给出了 400 Bad Request 错误,并且似乎是验证器错误。这是我发送的 POST 请求 JSON 数据。已经过去了漫长的一天,但仍然没能弄清楚这一点。下面我列出了我的架构和路线。

我还使用诸如 body-parser 和 node-restful 之类的软件包

{
    "aTitle": "THIS IS A TEST",
    "aBody": "This is a body test"
}
<小时/>
  {
      "message": "Questions validation failed",
      "name": "ValidationError",
      "errors": {
        "answers.aBody": {
          "message": "Path `answers.aBody` is required.",
          "name": "ValidatorError",
          "properties": {
            "type": "required",
            "message": "Path `{PATH}` is required.",
            "path": "answers.aBody"
          },
          "kind": "required",
          "path": "answers.aBody"
        },
        "answers.aTitle": {
          "message": "Path `answers.aTitle` is required.",
          "name": "ValidatorError",
          "properties": {
            "type": "required",
            "message": "Path `{PATH}` is required.",
            "path": "answers.aTitle"
          },
          "kind": "required",
          "path": "answers.aTitle"
        },
        "qBody": {
          "message": "Path `qBody` is required.",
          "name": "ValidatorError",
          "properties": {
            "type": "required",
            "message": "Path `{PATH}` is required.",
            "path": "qBody"
          },
          "kind": "required",
          "path": "qBody"
        },
        "qTitle": {
          "message": "Path `qTitle` is required.",
          "name": "ValidatorError",
          "properties": {
            "type": "required",
            "message": "Path `{PATH}` is required.",
            "path": "qTitle"
          },
          "kind": "required",
          "path": "qTitle"
        }
      }
    }
<小时/>
// Dependencies
var restful = require('node-restful');
// Database
var mongoose = restful.mongoose;

var Schema = mongoose.Schema;

// Question Schema
var QuestionSchema = new Schema({
  qTitle: {
    type: String,
    required: true
  },
  qBody: {
    type: String,
    required: true
  },
  created_at: {
    type: Date
  },
  updated_at: {
    type: Date
  },
  // Relationship to the Question or child of the question
  answers: {
    aTitle: {
      type: String,
      required: true
    },
    aBody: {
      type: String,
      required: true
    },
    created_at: Date,
    updated_at: Date
  }
});


// Export the question schema
module.exports = restful.model('Questions', QuestionSchema);
<小时/>
'use strict';
var express = require('express');

var router = express.Router();

var Question = require('../models/question');

Question.methods(['get', 'put', 'post', 'delete']);
Question.register(router, '/questions');
Question.register(router, '/questions/:id/answers');


// Exports the router
module.exports = router;

最佳答案

根据错误消息和问题架构(看看有 required: true 的),您需要发送的 POSTed 请求 JSON 数据至少是这样的:

{
    "qTitle": "this is question title",
    "qBody": "this is question body",
    "answers": {
        "aTitle": "THIS IS A TEST",
        "aBody": "This is a body test"
    }
}

关于javascript - 验证器错误 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40390796/

相关文章:

javascript - 为什么向 Observable 添加属性时会出现 TypeError

javascript - 应用于元素后,innerHTML 文本很快消失

javascript - 如何将 JSON 对象作为新级别添加到另一个 JSON 对象?

json - 不正确的 JSON 被解码为没有错误的结构

javascript - 使用数据库查询作为 promise

javascript - typescript 。如何声明 DOM 集合?

javascript - HTML onKeyDown-事件调用函数和按钮点击

sql-server - 找不到数据源名称并且没有指定nodejs的默认驱动程序

javascript - 使用 Knockout.JS 从 JSON 数据生成下载链接

javascript - 使用 JavaScript 读取在浏览器中打开的页面