Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

findById read empty arrays when the items are defined with a "_id" field #72

Closed
pgsfredda opened this issue Nov 4, 2017 · 3 comments
Closed

Comments

@pgsfredda
Copy link

pgsfredda commented Nov 4, 2017

Hi.
I'm using yet typegoose 3.5.2 because 3.6.0 don't works for me (see #68).

I defined a model with the "_id" field to simplify the configuration of data on db. This is not frequently but it works and it is admitted with mongoose.

When I load in memory a doc defined with an array of items wich are defined with an "_id" field, the array is empty.

Below the source file.

import * as tg from 'typegoose';

export class perm extends tg.Typegoose {
    @tg.prop() routerFunction : string;
    @tg.prop() permit: boolean;
}

export const permModel = new perm().getModelForClass(perm);


export class group extends tg.Typegoose {
    @tg.prop( { required: true, index: true, unique: true } ) _id: string;
    @tg.prop( { required: true, index: true, unique: true } ) name : string;
    @tg.arrayProp( { itemsRef: perm } ) perms ?: tg.Ref<perm>[];
}

export const groupModel = new group().getModelForClass(group);

export class admin extends tg.Typegoose {
    @tg.prop() name : string;
    @tg.arrayProp( { itemsRef: group } ) groups ?: tg.Ref<group>[];
    @tg.arrayProp( { itemsRef: perm } ) perms ?: tg.Ref<perm>[];
}

export const adminModel = new admin().getModelForClass(admin);

adminModel.findById('an id').exec((err, data) => {
   console.log(data); // prints correctly the perms array data but an empty groups array
}
@derchirurg
Copy link

I found out: do not define _id as prop. Just use it as property and it should work.

@pgsfredda
Copy link
Author

pgsfredda commented Jan 15, 2018

Thank you @derchirurg but your answer is not complete.
I tried to remove the _id definition field but:
if I save an _id as string I can't use that to find the recors and I can't load that field with typegoose using find or findbyid.

With other mongo client I can use that _id also when ai save it as ObjectID or as string because mongoose convert it automatically. So, I think this is a typegoose issue.

This is a problem for that records that you have to save before run the app for configuring something on the db. In that case is usefull to use a known _id (as string). But, when the app is running, is usefull use the automatic ObjectID generation.

I tried also this definition:

@tg.prop({ required: false, index: true}) _id: string | mongoose.Schema.Types.ObjectId;

but this solution cause an exeption:

In property _id: Object is not a primitive type nor a Typegoose schema (Not extending it).

@hasezoey
Copy link
Contributor

@Ben305 this can be closed as "stale" & "fixed?" - no-one encountered this again since January 2018, and should be working with v6

@Ben305 Ben305 closed this as completed Sep 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants