“instanceof” 的右侧不是对象
我正在练习 nestjs 以便将 ReST API 转换为 graphql,但是每当我尝试使用游乐场从我的 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 吗?
问题可能来自 这里
您需要将 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,以避免将来出现这种情况。