TypeError: Cannot read property 'object' of undefined
See original GitHub issuevalidation.pipe.ts
import {
PipeTransform,
Injectable,
ArgumentMetadata,
BadRequestException,
} from "@nestjs/common";
@Injectable()
export class JoiValidationPipe implements PipeTransform {
constructor(private schema) {}
transform(value: any, metadata: ArgumentMetadata) {
const { error } = this.schema.validate(value);
if (error) {
throw new BadRequestException("Validation failed");
}
return value;
}
}
schema.js
import Joi from "@hapi/joi";
export const createUserSchema: Joi.Schema = Joi.object().keys({
name: Joi.string().required(),
age: Joi.number()
.min(0)
.max(150)
.required(),
});
controller.ts
import {
Body,
Controller,
Get,
HttpStatus,
Post,
UsePipes,
} from "@nestjs/common";
import { ApiBearerAuth, ApiResponse, ApiTags } from "@nestjs/swagger";
import { JoiValidationPipe } from "../../common/pipes/validation.pipe";
import { UserEntityDto } from "./entity/user.entity";
import { createUserSchema } from "./validations/user.validations";
@Controller("users")
@ApiTags("users")
@ApiBearerAuth()
export class UserController {
public constructor() {}
@Post()
@UsePipes(new JoiValidationPipe(createUserSchema))
async create(@Body() createUserDto: UserEntityDto) {
console.log(createUserDto);
}
}
packages
"@nestjs/common": "^7.0.0",
"@hapi/joi": "^17.1.1",
When I run Getting the following error
TypeError: Cannot read property 'object' of undefined
What is the mistake here.
Please help me to solve this.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError: Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >React v16: Uncaught TypeError: Cannot read property 'object ...
This error appears because of you use the 16th version of React.js. In this blog post (announce of React.js version 16) you can...
Read more >Uncaught TypeError: Cannot read property of undefined In
JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or...
Read more >[Solved] Cannot read Properties of Undefined in JavaScript
The "Cannot read properties of undefined" error occurs when you try to access a property or a method on a variable that stores...
Read more >How to Avoid Getting 'Cannot read property of undefined' in ...
This error occurs when you try to read or access a property on an object that is undefined . Another common case that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
import * as Joi from '@hapi/joi';
instead of
import Joi from "@hapi/joi";
I also have the same problem. What is your solution?