使用 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/