Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在DTO中定义ObjectId&;在NestJS Mongoose中获取关系数据的正确查询是什么?_Node.js_Mongodb_Mongoose_Graphql_Nestjs - Fatal编程技术网

Node.js 如何在DTO中定义ObjectId&;在NestJS Mongoose中获取关系数据的正确查询是什么?

Node.js 如何在DTO中定义ObjectId&;在NestJS Mongoose中获取关系数据的正确查询是什么?,node.js,mongodb,mongoose,graphql,nestjs,Node.js,Mongodb,Mongoose,Graphql,Nestjs,我正在使用NestJS、Angular、GraphQL和MongoDB建立一个应用程序,并且是这些堆栈的新成员 我有两个集合,名为任务和状态,包含样本数据 任务 身份 [{ "_id": { "$oid": "5f91375d1163a739c43fc9af" }, "StatusDesc": "Done" }] 以下是在NestJS中定义的模式 import { Sche

我正在使用NestJS、Angular、GraphQL和MongoDB建立一个应用程序,并且是这些堆栈的新成员 我有两个集合,名为
任务
状态
,包含样本数据

任务

身份

[{
  "_id": {
    "$oid": "5f91375d1163a739c43fc9af"
  },
  "StatusDesc": "Done"
}]

以下是在NestJS中定义的模式

import { Schema, Types } from 'mongoose';
import { MongooseModule } from '@nestjs/mongoose';

export const TaskSchema = new Schema({
    TaskDesc: String,
    StartedDate: String,
    EndDate: String,
    StatusId:  
    {
        type: Types.ObjectId, 
        ref: 'Statuses'
    }
});

export const StatusSchema = new Schema({
    StatusDesc: String
});

export const SchemaGroups = MongooseModule.forFeature([
    {name: 'Tasks', schema: TaskSchema, collection: 'Tasks'},
    {name: 'Statuses', schema: StatusSchema, collection: 'Statuses'}
]);

DTO

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

@ObjectType()
export class TasksDTO {
  @Field(() => ID)
  id?: string;
  @Field()
  TaskDesc: string;
  @Field()
  StartedDate: string;
  @Field()
  EndDate: string;
  @Field()
  StatusId: string;
}

@ObjectType()
export class StatusDTO {
  @Field(() => ID)
  readonly id?: string;
  @Field()
  readonly StatusDesc: string;
}

模型

import { Document, Schema } from 'mongoose';
export interface Tasks extends Document {
    readonly TaskDesc : string,
    readonly StartedDate: string,
    readonly EndDate: string,
    readonly StatusId: Schema.Types.ObjectId
}
export interface Status extends Document {
    readonly StatusDesc : string
}
分解器

@Resolver('Tasks')
export class ListTodoResolver {
    constructor(private readonly todoItemsService: TodolistService){

    }

    @Query(() => [TasksDTO])
    async Tasks(): Promise<TasksDTO[]> {
      return await this.todoItemsService.getAllTasks();
    }

    @Query(() => [StatusDTO])
    async Statuses(): Promise<StatusDTO[]>{
      return await this.todoItemsService.getAllStatuses();
    }
}

错误已经显示在您应该修复它的位置

类型“Tasks”不可分配给类型“TasksDTO”。属性“StatusId”的类型不兼容。类型“ObjectId”不可分配给类型“string”

这意味着你的DTO应该是

@ObjectType()
export class TasksDTO {
  @Field(() => ID)
  id?: string;
  @Field()
  TaskDesc: string;
  @Field()
  StartedDate: string;
  @Field()
  EndDate: string;
  @Field()
  StatusId: objectId; // change here
}

不确定DTO中的对象Id类型,请尝试在文档中查找DTO是否有对象Id类型。如果没有,则应将所有位置的StatusId更改为string,这样应该可以解决问题。

错误已经显示在您应该修复它的位置

类型“Tasks”不可分配给类型“TasksDTO”。属性“StatusId”的类型不兼容。类型“ObjectId”不可分配给类型“string”

这意味着你的DTO应该是

@ObjectType()
export class TasksDTO {
  @Field(() => ID)
  id?: string;
  @Field()
  TaskDesc: string;
  @Field()
  StartedDate: string;
  @Field()
  EndDate: string;
  @Field()
  StatusId: objectId; // change here
}
不确定DTO中的对象Id类型,请尝试在文档中查找DTO是否有对象Id类型。如果没有,则应将所有位置的StatusId更改为string,这样应该可以工作

[{
  "_id": "5f9138f71163a739c43fc9b3",
  "TaskDesc": "Meeting with Brian",
  "StartedDate": "2020-10-22T07:42:40Z",
  "EndDate": "2020-10-22T10:42:40Z",
  "StatusDesc": "Done"
}]
@ObjectType()
export class TasksDTO {
  @Field(() => ID)
  id?: string;
  @Field()
  TaskDesc: string;
  @Field()
  StartedDate: string;
  @Field()
  EndDate: string;
  @Field()
  StatusId: objectId; // change here
}