Ad

Validation Error: Path Is Required But Variable Is Neither Null Nor Empty

- 1 answer

I tested of my create method worked but when I did, it gave me back this error:

makeChallenge of challengeService { ValidationError: challenges validation failed: minimumLevel: Path minimumLevel is required., endDate: Path endDate is required., startDate: Path startDate is required., objective: Path objective is required., description: Path description is required., name: Path name is required.

But when I logged the request's body, all these parameters were filled in. When I left out one parameter, then the error changed to only that parameter that is required.

Model

import mongoose, { Document, Schema, Model } from 'mongoose';

export interface Challenge {
  _id: any;
  name: string;
  description: string;
  objective: string;
  startDate: Date;
  endDate: Date;
  minimumLevel: number;
}

export interface ChallengeDocument extends Challenge, Document {}

const schema = new Schema(
  {
    name: { type: String, required: true },
    description: { type: String, required: true },
    objective: { type: String, required: true },
    startDate: { type: Date, required: true },
    endDate: { type: Date, required: true },
    minimumLevel: { type: Number, required: true }
  },
  { _id: true, timestamps: true }
);

export const model = mongoose.model<ChallengeDocument>('challenges', schema);

Models

import * as user from './modules/user/model';
import * as challenge from './modules/challenge/model';
import * as contract from './modules/contract/model';

export type Models = typeof models;

const models = {
  user,
  challenge,
  contract
};

export default models;

Service

import { Router } from 'express';
import models from '../models';

const routes = Router();

const stringToDate = (string: string): Date => {
  let subStringArray = string.split('-');
  let intList: number[] = [];
  subStringArray.forEach(str => {
    intList.push(Number.parseInt(str));
  });
  console.log(intList);
  let date: Date = new Date(intList[0], intList[1], intList[2]);
  return date;
};

routes.post('/makeChallenge', async (req, res) => {
  console.log(req.body);
  const challenge = await models.challenge.model
    .create(
      {
        name: req.body.name,
        description: req.body.description,
        objective: req.body.objective,
        startDate: stringToDate(req.body.startDate),
        endDate: stringToDate(req.body.endDate),
        minimumLevel: Number.parseInt(req.body.minimumLevel)
      },
      { new: true }
    )
    .catch(e => console.log('makeChallenge of challengeService', e));
  res.send(challenge);
});

export default routes;

Postman request:

post to http://localhost:xxxx/makeChallenge

{
    "name" : "TestChallenge",
    "description" : "This is to test the api",
    "objective" : "Make the api work",
    "startDate" : "2019-9-23",
    "endDate" : "2019-12-20",
    "minimumLevel" : 1
}
Ad

Answer

In mongoose docs I didnt find such an option for model.create, can you remove the { new: true } and try?

Ad
source: stackoverflow.com
Ad