-
-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isRefType function fails to identify wrapped ObjectId instances #924
Comments
This comment was marked as outdated.
This comment was marked as outdated.
sorry for the earlier message, i didnt read correctly. yes, currently |
I get it fron nestjs resolveField: @ResolveField(returns => Company)
company(
@Parent() news: News,
@Args('lang') lang: string,
@Loader(CompanyDataLoader.name)
companyDataLoader: Dataloader<IntlDataLoaderKey<Company>, Company>,
): Promise<Company> {
const doc: any = news.company; //actually company is: company?: Ref<Company>; in the news model.
``` |
thanks, but i do not really understand what is coming from where, is the Note: i mean the types at runtime not just typescript types |
Mongoose Document. const news = await this.newsModel.findOne<NewsDocument>(criteria); |
then the check should work without problems, to my knowledge at least; unless you somehow have mis-matching mongoose versions installed? |
I need to debug it and see how it came from, under the hood. This issue starts after several library updates. I will check more thoroughly. |
Guessing from the screenshots, i think those ObjectIds are a direct instance of I dont quite know the semantics of Maybe check that the returned |
You are right! I finally found the problem. @Injectable()
export class TypegooseInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(map(data => {
if (Array.isArray(data)) {
return data.map(item => {
return item instanceof Model ? toGraph(item) : item;
});
}
// **this fix the problem**
if (Array.isArray(data.edges)) {
for (const edge of data.edges) {
edge.node = edge.node ? toGraph(edge.node) : edge.node;
}
}
if (data instanceof Model) {
return toGraph(data);
}
return data;
}));
}
}
function toGraph(doc: Document): any {
const convertedDocument = doc.toObject();
const DocumentClass = getClass(doc);
Object.setPrototypeOf(convertedDocument, DocumentClass.prototype);
return convertedDocument;
} Thank you so much for your help. Sorry, but sometimes it's pure hell. |
Versions
NodeJS: 20.11.1
Typegoose (NPM): 12.0.0
mongoose: 8.0.3
mongodb: MongoDB
What is the Problem?
The isRefType function in Typegoose seems to fail to identify ObjectId instances that are "wrapped" (boxed) by MongoDB. When checking if a document property is an ObjectId reference type, the isRefType function returns false even though the property appears to be a valid ObjectId.
Code Example
Do you know why it happens?
The issue appears to be caused by the way MongoDB "wraps" (boxes) ObjectId instances when returning them from the database. The isRefType function checks if the document property is an instance of mongoose.Types.ObjectId using the instanceof operator. However, the wrapped ObjectId instance is not a direct instance of the ObjectId class, so the instanceof check fails.
One potential solution could be to modify the isRefType function to handle these wrapped ObjectId instances. For example, by checking if the object has a _bsontype property with the value 'ObjectID', which is a known characteristic of wrapped ObjectId instances.
I would appreciate any guidance or insights you can provide on this issue and the appropriate way to handle wrapped ObjectId instances in Typegoose.
The text was updated successfully, but these errors were encountered: