You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.3.0
Node.js version
20.12.2
MongoDB server version
Cloud Atlas
Typescript version (if applicable)
5.4.3
Description
Methods that have been "overloaded" or replaced by discriminated child models are ignored after the app is restarted.
With a base model and discriminated model, the discriminated one can be inserted into Mongo, then retrieved, then the discriminated methods can be triggered. However, if the app is closed and started again, any existing documents in the database are then retrieved as the base model instead of the discriminated model so the base methods are called instead.
I've even tried getting a raw object and then re-hydrating the document. Even thought the kind is there, it hydrates a base model rather than the discriminated model.
const gameData: IGameDataDocument = await GameDataModel.findOne({gameId: params.gameid}).exec();
console.log("gameData", gameData);
const gameDataObject = gameData.toObject();
console.log("gameDataObject", gameDataObject);
const gameDataHydrated: IGameDataDocument = GameDataModel.hydrate(gameDataObject);
console.log("gameDataHydrated", gameDataHydrated)
const gameDataResponse = await gameData.CreateDataResponse(); // prints: CreateDataResponse: Generic game
const gameDataResponseHydrated = await gameDataHydrated.CreateDataResponse(); // prints: CreateDataResponse: Generic game
console.log("responses", gameDataResponse, gameDataResponseHydrated); // Lost all the special details
I've figured it out. When the app is restarted, the discriminated models are no longer "registered". They would only be registered with Mongoose when they are first accessed.
To fix this, I have created a method that initialises all the discriminators on dbConnect() just by referencing each model. Maybe there's a more generic version or different way I should structure my code?
So the issue is that your discriminators are defined in a file that you don't import until later? Or how are you actually registering your discriminated models?
The dbConnect() method sounds like a reasonable idea: you need to make sure you actually define all your models and discriminators, ideally when your app starts.
vkarpov15
added
the
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
label
Apr 25, 2024
Prerequisites
Mongoose version
8.3.0
Node.js version
20.12.2
MongoDB server version
Cloud Atlas
Typescript version (if applicable)
5.4.3
Description
Methods that have been "overloaded" or replaced by discriminated child models are ignored after the app is restarted.
With a base model and discriminated model, the discriminated one can be inserted into Mongo, then retrieved, then the discriminated methods can be triggered. However, if the app is closed and started again, any existing documents in the database are then retrieved as the base model instead of the discriminated model so the base methods are called instead.
Steps to Reproduce
Base documene
Discriminated documents
Usage
Expected Behavior
The discriminated methods should be called even if the document wasn't created in this session.
The text was updated successfully, but these errors were encountered: