【问题标题】:"TypeError: Cannot read property 'prototype' of undefined" nestjs with graphql and mongodb“TypeError:无法使用graphql和mongodb读取未定义的属性'原型'”nestjs
【发布时间】:2022-02-04 23:03:13
【问题描述】:

我遇到了这个错误,我不确定如何解决它。 当我尝试运行 getLesson() 函数时出现错误,我只是想通过公共 ID 获取课程。

错误和代码如下所示

{ “错误”:[ { "message": "无法读取未定义的属性 'prototype'", “地点”:[ { “线”:2, “列”:3 } ], “小路”: [ “课” ], “扩展”:{ “代码”:“INTERNAL_SERVER_ERROR”, “例外”: { “堆栈跟踪”: [ “TypeError:无法读取未定义的属性‘原型’”, "在 FindCursor.cursor.toArray (C:\Users\LENOVO\Projects\practice\graphql-mongodb\src\entity-manager\MongoEntityManager.ts:704:37)", "在 MongoEntityManager。(C:\Users\LENOVO\Projects\practice\graphql-mongodb\src\entity-manager\MongoEntityManager.ts:189:46)", " 在步骤 (C:\Users\LENOVO\Projects\practice\graphql-mongodb\node_modules\tslib\tslib.js:143:27)", " 在 Object.next (C:\Users\LENOVO\Projects\practice\graphql-mongodb\node_modules\tslib\tslib.js:124:57)", " 完成时 (C:\Users\LENOVO\Projects\practice\graphql-mongodb\node_modules\tslib\tslib.js:114:62)", “在 processTicksAndRejections (internal/process/task_queues.js:95:5)” ] } } } ], “数据”:空 }

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);
  } }

课程类型.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;
}

【问题讨论】:

    标签: node.js typescript mongodb graphql nestjs


    【解决方案1】:

    mongodb v4有问题需要降级到v3

    yarn add mongodb@3
    npm install mongodb@3
    

    我使用的是 3.7.1 版本,它运行良好

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

    【讨论】:

      猜你喜欢
      • 2021-10-30
      • 2021-11-20
      • 2021-08-01
      • 2022-01-12
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2019-09-24
      相关资源
      最近更新 更多