[help] InferSchemaType, accessing _id field
See original GitHub issuePrerequisites
- I have written a descriptive issue title
Mongoose version
6.4.0
Node.js version
18.3
MongoDB version
Unsure
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
arch-5.18.5 something
Issue
Using InferSchemaType
to create the schema interface type, how do I access the _id
field? If I put it in the schema definition, I have to also set it explicitly when inserting new documents. If I don’t put it there, InferSchemaType
does not include it in the interface.
What do I do? I’d like to know the same for .save()
and so on. Am I supposed to do a type union?
Code:
import { Document, InferSchemaType, Schema } from "mongoose"
const mySchema = new Schema({
foo: { type: String, required: true }
})
type MySchemaType_Problematic = InferSchemaType<typeof mySchema>
// MySchemaType_Problematic is missing the _id field along with the save() method etc.
// Workaround:
type MySchemaType_Workaround = InferSchemaType<typeof mySchema> & Document
// MySchemaType_Workaround can now be used properly, with _id and save() etc.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
tsc doesn't recognize virtuals on mongoose scheme
TSC doesn't recognize them as a field in the interface. I tried in both suggested manners (see code below). import {connect, InferSchemaType, ...
Read more >Schemas in TypeScript - Mongoose
Mongoose schemas are how you tell Mongoose what your documents look like. Mongoose schemas are separate from TypeScript interfaces, so you need to...
Read more >TypeScript: timestamps not inferred from schema · Issue #12069
I realised mongoose has just recently migrated to typescript and isn't fully feature complete yet so appreciate any help.
Read more >How to use NodeJs mongoose to find the object ID
All we need to do is specify the value of the _id field, and the findById() method will retrieve the document. Let's discuss...
Read more >Why reflect-metadata suc*s - DEV Community
@Model() class userModel /* decorators are nice syntax sugar ^^ */ @Field() id: string /* problem is that business logic is coded in ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@philiparvidsson can you please provide some code samples? That would be helpful for me to understand what you’re trying to do.
From OP, I’d recommend doing:
For “TL;DR: mongoose.Schema.Types.ObjectId should be inferred to mongoose.Types.ObjectId for fields”, this was fixed in 6.4.2 with #12007.
Those two issues are fixed, and there aren’t enough code samples for this to be actionable. So I’m going to close this. Please re-open with a complete code sample that we can run to demonstrate the issue you’re seeing, if you’re still having issues.