'instanceof' 的右侧不是对象

Right-hand side of 'instanceof' is not an object

我正在练习 nestjs 以将 ReST API 转换为 graphql,但每当我尝试使用 playground 从我的 GraphQL API 获取数据时,我都会收到此错误:

"errors": [ { "message": "Right-hand side of 'instanceof' is not an object", "locations": [ { "line": 2, "column": 3 } ], "path": [ "lesson" ], "extensions": { "code": "INTERNAL_SERVER_ERROR", "exception": { "stacktrace": [ "TypeError: Right-hand side of 'instanceof' is not an object", " at MongoEntityManager. (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\typeorm\entity-manager\MongoEntityManager.js:159:51)", " at step (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:143:27)", " at Object.next (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:124:57)", " at C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:117:75", " at new Promise ()", " at Object.__awaiter (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\tslib\tslib.js:113:16)", " at MongoEntityManager.findOne (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\typeorm\entity-manager\MongoEntityManager.js:153:24)", " at MongoRepository.findOne (C:\Users\Oluyinka\Desktop\graphql-mongodb\node_modules\typeorm\repository\MongoRepository.js:57:29)", " at LessonService.getLesson (C:\Users\Oluyinka\Desktop\graphql-mongodb\dist\lesson\lesson.service.js:26:44)", " at LessonResolver.lesson (C:\Users\Oluyinka\Desktop\graphql-mongodb\dist\lesson\lesson.resolver.js:24:35)" ] } } } ], "data": null }

下面是我的代码:

lesson.entity.ts

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

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

  @PrimaryColumn()
  id: string;

  @Column()
  name: string;

  @Column()
  startDate: string;

  @Column()
  endDate: string;
}

lesson.service.ts

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

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

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

  async getLessons(): Promise<Lesson[]> {
    return this.lessonRepository.find();
  }

  async createLesson(name, startDate, endDate): Promise<Lesson> {
    const lesson = this.lessonRepository.create({
      id: uuid(),
      name,
      startDate,
      endDate,
    });
    return await this.lessonRepository.save(lesson);
  }
}

lesson.resolver.ts

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

@Resolver((of) => LessonType)
export class LessonResolver {
  constructor(private lessonService: LessonService) {}
  @Query((returns) => LessonType)
  lesson(@Args('id') id: string) {
    return this.lessonService.getLesson(id);
  }

  @Query((returns) => [LessonType])
  lessons() {
    return this.lessonService.getLessons();
  }

  @Mutation((returns) => LessonType)
  createLesson(
    @Args('name') name: string,
    @Args('startDate') startDate: string,
    @Args('endDate') endDate: string,
  ) {
    return this.lessonService.createLesson(name, startDate, endDate);
  }
}

lesson.type.ts

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

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

  @Field()
  name: string;

  @Field()
  startDate: string;

  @Field()
  endDate: string;
}

最近似乎对 MongoDB 驱动程序进行了一些重大更改。您在使用 mongo 4.x 吗?

问题可能来自 here

您需要将 mongo 版本固定到 3.x,直到它得到支持。

相关 Github 个问题:

您的 @nestjs/common@nestjs/core@nestjs/platform-express 版本恢复到主要版本 7,但您正在使用 @nestjs/graphql 并且 @nestjs/typeorm 正在使用主要版本 8。确保这些主要版本保持同步,否则你最终会遇到这样的错误。

此外,TypeORM 不支持 Mongo v4,因此请确保您也安装了 mongo v3,正如 Jesse 提到的那样。


如果我不得不猜测发生了什么,您在 v7 上使用 Nest CLI 生成了一个新项目,然后为 Nest 安装了 graphql 和 typeorm 包,这将安装它们的 @latest,v8。我建议将您的 Nest CLI 升级到版本 8 以避免将来出现这种情况。