'"TypeError: Cannot read property 'prototype' of undefined" nestjs with graphql and mongodb

I am having this error and I am unsure how to resolve it. The error comes when I try to run getLesson() function, in which I am just trying to get the lesson by a public id.

The error and code are shown below

{ "errors": [ { "message": "Cannot read property 'prototype' of undefined", "locations": [ { "line": 2, "column": 3 } ], "path": [ "lesson" ], "extensions": { "code": "INTERNAL_SERVER_ERROR", "exception": { "stacktrace": [ "TypeError: Cannot read property 'prototype' of undefined", " at FindCursor.cursor.toArray (C:\Users\LENOVO\Projects\practice\graphql-mongodb\src\entity-manager\MongoEntityManager.ts:704:37)", " at MongoEntityManager. (C:\Users\LENOVO\Projects\practice\graphql-mongodb\src\entity-manager\MongoEntityManager.ts:189:46)", " at step (C:\Users\LENOVO\Projects\practice\graphql-mongodb\node_modules\tslib\tslib.js:143:27)", " at Object.next (C:\Users\LENOVO\Projects\practice\graphql-mongodb\node_modules\tslib\tslib.js:124:57)", " at fulfilled (C:\Users\LENOVO\Projects\practice\graphql-mongodb\node_modules\tslib\tslib.js:114:62)", " at processTicksAndRejections (internal/process/task_queues.js:95:5)" ] } } } ], "data": null }

lesson.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Lesson } from './lesson.entity';
import { Repository } from 'typeorm';
import { v4 as uuid } from 'uuid';

@Injectable()
export class LessonService {
  constructor(
    @InjectRepository(Lesson)
    private lessonRepository: Repository<Lesson>,
  ) {}

  async getLesson(id: string): Promise<Lesson> {
    return this.lessonRepository.findOne({ id });
  }
}

lesson.resolver.ts

import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';
import { LessonService } from './lesson.service';
import { LessonType } from './lesson.type';

@Resolver((of) => LessonType)
export class LessonResolver {
  constructor(private lessonService: LessonService) {}

  //queries are used to retrieve data and mutations are user to create or modify data
  @Query((returns) => LessonType)
  lesson(@Args('id') id: string) {
    return this.lessonService.getLesson(id);
  } }

lesson.type.ts

import { ObjectType, Field, ID } from '@nestjs/graphql';

@ObjectType('Lesson')
export class LessonType {
  @Field((type) => ID)
  id: string;

  @Field()
  name: string;

  @Field()
  startDate: string;

  @Field()
  endDate: string;
}

lesson.entity.ts

import { Entity, PrimaryColumn, Column, ObjectIdColumn } from 'typeorm';

@Entity()
export class Lesson {
  @ObjectIdColumn()
  _id: string;

  @PrimaryColumn()
  id: string;

  @Column()
  name: string;

  @Column()
  startDate: string;

  @Column()
  endDate: string;
}


Solution 1:[1]

mongodb v4 has some problem so you need to downgrade to v3

yarn add mongodb@3
npm install mongodb@3

I use version 3.7.1 and it work fine

here: TypeORM error with MongoDB: .find() does not work, error: TypeError: Cannot read property 'prototype' of undefined

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1