当我尝试发布问题ID的答案时使用Postman,它会给出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;
答案 0 :(得分:2)
根据错误消息和问题架构(查看具有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"
}
}