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.

[help] InferSchemaType, accessing _id field

See original GitHub issue

Prerequisites

  • 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:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Jun 22, 2022

@philiparvidsson can you please provide some code samples? That would be helpful for me to understand what you’re trying to do.

0reactions
vkarpov15commented, Jul 2, 2022

From OP, I’d recommend doing:

type MySchemaType_Workaround = HydratedDocument<InferSchemaType<typeof mySchema>>

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.

Read more comments on GitHub >

github_iconTop 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 >

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