Mongoose Property 'x' does not exist on type 'Document'
The mongoose.model
method accepts a type that defaults to mongoose.Document
, which won't have properties you want on your User
document.
To fix this, create an interface that describes your schema and extends mongoose.Document
:
export interface UserDoc extends mongoose.Document { email: { type: string; unique: boolean; required: boolean; } ...}
Then, pass that through as the type for your model:
export = mongoose.model<UserDoc>('User', userSchema);
When I defined my entity interface I had it extending "Document" but had not imported anything.
When I crtl+click on it though I can see that it incorrectly thinks I was referring to the built-in node object "Document":
/** Any web page loaded in the browser and serves as an entry point into the web page's content, which is the DOM tree. */interface Document extends Node, DocumentAndElementEventHandlers, DocumentOrShadowRoot, GlobalEventHandlers, NonElementParentNode, ParentNode, XPathEvaluatorBase {
After I explicitly told it to use Document from the mongoose library things worked. 👍
import { Document } from 'mongoose';