繁体   English   中英

错误 TS2345:“FlattenMaps”类型的参数 - Typescript 编译失败

[英]error TS2345: Argument of type 'FlattenMaps' - Typescript Compile failed

由于嵌套模式,typescript(npm run build)的编译失败。 步骤 (hubs.push(h.toJSON());) 因 TS2345 错误代码而失败。 不确定缺少什么,我正在尝试升级 nodejs 和 mongodb 版本。 请帮我解决这个问题:

失败的代码步骤:控制器尝试将所有集线器收集为 JSON 有效负载:

            const result = org.toJSON();
            const hubs: HubRep[] = [];

            const hs = await Hub.find({ "organization.ref": org._id }, { _id: 1, name: 1 });
            hs.forEach(h => {
             
            
                hubs.push(h.toJSON());
            });

架构代码:

import * as mongoosePaginate from "mongoose-paginate";
import { EntityModel, Entity, entitySchemaHelper, DbRef } from "../../lib/data/entity";
import { Organization, OrganizationModel } from "../../iam/models/organization";
import { CityModel, citySchema } from "./city";

export interface HubRep extends EntityModel {
    name: string;
    displayName: string;
    city: CityModel;
    organization: DbRef<OrganizationModel, "Organization">;
}

export interface HubModel extends HubRep, Entity {
}

const hubSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    displayName: {
        type: String,
        required: true
    },
    city: citySchema,
    organization: {
        kind: {
            type: String,
            required: true,
            default: "Organization"
        },
        ref: {
            type: String,
            refPath: "organization.kind",
            required: true
        }
    }
}

错误 TS2345:'FlattenMaps<{ 名称:字符串类型的参数; 显示名称:字符串; 城市:城市模型; 组织:DbRef<组织模型,“组织”,字符串>; _revision?:数字; _createdAt?: 日期; ... 4 更多...; _version?: number; }>' 不可分配给“HubRep”类型的参数。 'city.$assertPopulated' 的类型在这些类型之间不兼容。 类型 'FlattenMaps<(<Paths = {}>(paths: string | string[]) => Omit<any, keyof Paths> & Paths)>' 不可分配给类型 '<Paths = {}>(paths: string | string[]) => 省略<any, keyof Paths> & Paths'。 类型 'FlattenMaps<(<Paths = {}>(paths: string | string[]) => Omit<any, keyof Paths> & Paths)>' 不提供与签名 '<Paths = {}>(paths: string | string[]): 省略<any, keyof Paths> & Paths'

似乎h.toJSON()正在返回FlattenMapshubs.push HubRep[]类型的 hubs.push 期待HubRep

为了进一步排除故障,可能值得将.forEach替换为:

const hubJsons = hs.map((hub) => hub.toJSON())

然后至少你可以看看 hubJsons 看看需要做什么才能把它变成HubRep[]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM