question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sub-schema Type Mismatch

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? Schema inside schema (sub-schema) type mismatch

If the current behavior is a bug, please provide the steps to reproduce.

There is a Schema referring to GeoLocation, as per mongoose documentation, this following PointSchema.ts file is created

// PointSchema.ts ======

import { Document, Schema } from "mongoose";

export interface IGeoPoint {
    type: string
    coordinates: Array<number>
}

export interface IPoint extends IGeoPoint, Document {
    getGeoCoordinates(): IGeoCoordinate
}

const pointSchema: Schema<IPoint> = new Schema({
    type: {
        type: String,
        enum: ['Point'],
        required: true
    },
    coordinates: {
        type: [Number],
        required: true
    }
});

export interface IGeoCoordinate {
    lat: number
    lon: number
}

pointSchema.methods.getGeoCoordinates = function (this:IPoint): IGeoCoordinate {
    return {
        lat: this.coordinates[0],
        lon: this.coordinates[1]
    }
}

export default pointSchema

and another file LocationModel.ts which uses this PointsSchema.ts is created

// LocationModel.ts ======

import { Document, model, Schema } from "mongoose";
import PointSchema, { IPoint, IGeoCoordinate, IGeoPoint } from "../Schemas/PointSchema";

export interface ILocationDocument extends Document {
    location: IPoint
    name: string
    address: string
    getGeoCoordinate(): IGeoCoordinate
}

const LocationSchema: Schema<ILocationDocument> = new Schema({
    location: PointSchema,      // <---- error at this line
    name: String,
    address: String
})

LocationSchema.methods.getGeoCoordinate = function (this: ILocationDocument): IGeoCoordinate {
    return this.location.getGeoCoordinates()
}

export default model<ILocationDocument>("location", LocationSchema, 'locations')

Error at the marked line is:


Argument of type '{ location: Schema<IPoint, Model<IPoint>>; name: StringConstructor; address: StringConstructor; }' is not assignable to parameter of type '{ [path: string]: SchemaDefinitionProperty<undefined>; }'.
  Property 'location' is incompatible with index signature.
    Type 'Schema<IPoint, Model<IPoint>>' is not assignable to type 'SchemaDefinitionProperty<undefined>'.
      Type 'Schema<IPoint, Model<IPoint>>' is not assignable to type 'Schema<Document<any>, Model<Document<any>>>'.
        Types of property 'methods' are incompatible.
          Type '{ getGeoCoordinates: () => IGeoCoordinate; type: string; coordinates: number[]; _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; ... 48 more ...; validateSync: (pathsToValidate?: string[] | undefined, opti...' is not assignable to type '{ _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; $isEmpty: (path: string) => boolean; ... 47 more ...; validateSync: (pathsToValidate?: string[] | undefined, options?: any) => CallbackError; } & { ...; }'.
            Type '{ getGeoCoordinates: () => IGeoCoordinate; type: string; coordinates: number[]; _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; ... 48 more ...; validateSync: (pathsToValidate?: string[] | undefined, opti...' is not assignable to type '{ [name: string]: (this: Document<any>, ...args: any[]) => any; }'.
              Index signatures are incompatible.
                Type '(this: IPoint, ...args: any[]) => any' is not assignable to type '(this: Document<any>, ...args: any[]) => any'.
                  The 'this' types of each signature are incompatible.
                    Type 'Document<any>' is not assignable to type 'IPoint'.

My tsconfig.json file:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./build",
    "rootDir": "./src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true
  }
}

What is the expected behavior? Sub-schema should be included by adapting the type of the schema

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Node: v14.7.0 mongoose: 5.11.11 @types/mongoose: 5.10.3 MongoDB: Atlas (version 4.2.11)

This issue was not seen in mongoose v5.9.13, and is seen in v5.11.11 (on probable enforcement of #9717)

Please update the documentation as per the updates and please let me know what I am missing here

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Feb 1, 2021

I took a closer look at this and managed to confirm the issue, looks like I needed to run just tsc, not tsc ./LocationModel.ts to get this error. It looks like we already fixed this issue in #9862, so this issue will be fixed in v5.11.15, which we will ship in the next couple of days. Thanks for your patience!

0reactions
anubhab-parallel-realitycommented, Feb 2, 2021

Thanks for the action, looking forward to the update

Read more comments on GitHub >

github_iconTop Results From Across the Web

error message for mongoose schema type mismatch
I tried adding a custom validator function and assigning it to the validate attribute, but unfortunately if there is a type mismatch, that ......
Read more >
Changes to Schemas and Subschemas - TechDocs
The primary tool for changing subschemas is the subschema compiler. Methods for Modifying. Depending on the type of change you want to make...
Read more >
Type Safety with JSON Subschema - Andrew Habib
Overview of JSON subschema checker. operators for preprocessing and prediction [8]. Any mismatch between training data and a pipeline,.
Read more >
org.everit.json.schema.CombinedSchema.getCriterion java code ...
@Override void describeTo(JSONObject obj) { obj.put("type", "mismatch"); obj.put("keyword", schema.getCriterion().toString()); obj.put("subSchema", ...
Read more >
13 type mismatch when clicking the price button in the POS ...
Legacy KB Id: 4515.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found