Ethan
Ethan

Reputation: 49

ValidatorError JSON

Upon using Postman when I try to POST an answer to the questions id it gives a 400 Bad Request error and it seems to be a validator error. Here is the POST request JSON data I am sending. It has been a long day and still havent been able to figure this out. Below I am listing my Schema and routes.

I am also using packages such as body-parser and 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;

Upvotes: 0

Views: 64

Answers (1)

elfan
elfan

Reputation: 1131

According to the error message and the question schema (look at the ones that have required: true), the POSTed request JSON data you need to send is at least like this:

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

Upvotes: 2

Related Questions