The compilation of the typescript (npm run build) failing due to nested schema. The step (hubs.push(h.toJSON());) is failing with TS2345 error code. Not sure what is missing, I am trying to upgrade nodejs and mongodb versions. Please help me troubleshoot this issue:
Code step which is failing: Conntroller trying to collect all the Hubs as a JSON paylod:
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());
});
The Schema Code:
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
}
}
}
error TS2345: Argument of type 'FlattenMaps<{ name: string; displayName: string; city: CityModel; organization: DbRef<OrganizationModel, "Organization", string>; _revision?: number; _createdAt?: Date; ... 4 more ...; _version?: number; }>' is not assignable to parameter of type 'HubRep'. The types of 'city.$assertPopulated' are incompatible between these types. Type 'FlattenMaps<(<Paths = {}>(paths: string | string[]) => Omit<any, keyof Paths> & Paths)>' is not assignable to type '<Paths = {}>(paths: string | string[]) => Omit<any, keyof Paths> & Paths'. Type 'FlattenMaps<(<Paths = {}>(paths: string | string[]) => Omit<any, keyof Paths> & Paths)>' provides no match for the signature '<Paths = {}>(paths: string | string[]): Omit<any, keyof Paths> & Paths'.
CodePudding user response:
Seems like h.toJSON()
is returning a FlattenMaps
but hubs.push
of type HubRep[]
is expecting HubRep
.
To troubleshoot further, it might be worth replacing the .forEach
with:
const hubJsons = hs.map((hub) => hub.toJSON())
Then at least you can look at hubJsons to see what needs doing to turn it into HubRep[]