【问题标题】:Mongoose: Property 'forEach' does not exist on type 'ObjectId | [BaseAction]'Mongoose:“ObjectId |”类型上不存在属性“forEach” [基础动作]'
【发布时间】:2022-02-07 16:58:02
【问题描述】:

我只是得到这个错误。 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]>;
}

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

【问题讨论】:

  • 你能分享你的模型的架构吗?看起来动作是单个引用,而不是引用数组

标签: typescript mongoose typegraphql


【解决方案1】:

为了在 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[];
}


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2018-11-09
    • 2018-03-03
    • 2020-12-12
    相关资源
    最近更新 更多