猫鼬:属性 'forEach' 在类型 'ObjectId | [BaseAction]' 上不存在

Mongoose: Property 'forEach' does not exist on type 'ObjectId | [BaseAction]'

我刚收到这个错误。 Property 'forEach' does not exist on type 'ObjectId | [BaseAction]'.

错误在最后一行,代码是:

    ServiceModel.findOne({name: "slack"}).populate({
        path: 'actions',
        model: 'BaseActionModel',
    }).then((res) => {
        if (!res?.actions) return // null verif

        res.actions.forEach((action: BaseAction) => {

我搜索了一下,但没有找到任何内容。 要添加更多上下文,我的模型是:

@ObjectType({description: 'Services'})
export class Service {
    @Field(() => ID)
    id!: string;

    @Field()
    @Property()
    name!: String;

    @Field()
    @Property()
    out_url!: String;

    @Field()
    @Property()
    in_url!: String;

    @Field((_type) => [BaseAction])
    @Property({ref: BaseAction})
    actions: Ref<[BaseAction]>;
}

如果需要,您可以索取更多代码

为了在 type-graphql 中执行 OneToMany 关系,您需要使用 typeorm。

import {ObjectType, Field, ID, InputType} from 'type-graphql';
import {prop as Property, getModelForClass, Ref} from '@typegoose/typegoose';
import {OneToMany} from "typeorm"; // import this

@ObjectType({description: 'Services'})
export class Service {
    @Field(() => ID)
    id!: string;

    @Field()
    @Property()
    name!: String;

    @Field()
    @Property()
    out_url!: String;

    @Field()
    @Property()
    in_url!: String;

    @Field((_type) => [BaseAction])
    // @ts-ignore type unused
    @OneToMany(type => BaseAction, action => action.id) // add this
    actions?: BaseAction[];
}