question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'object' of undefined

See original GitHub issue

validation.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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

99reactions
rabota-alexeycommented, Dec 13, 2020

import * as Joi from '@hapi/joi';

instead of import Joi from "@hapi/joi";

1reaction
isLzgcommented, Dec 12, 2020

I also have the same problem. What is your solution?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found