简体   繁体   中英

Typegoose class defined static method returns empty array

When I use findByUserId defined static method, I get empty array value whereas if I use the model directly it will retrieve successfully the object.

    export default class Cart {
    
        @prop({required: true, unique: true})
        public userId!: String
    
        @prop()
        public items?: [Item]
    
        public static async findByUserId(this: ReturnModelType<typeof Cart>, userId: string) {
            try {
                return await this.find({itemId: userId});
            } catch (err) {
                return null;
            }
        }
    }

    export const CartModel = getModelForClass(Cart);
    
    // RETURNS EMPTY ARRAY
     const user = await CartModel.findByUserId("asdasdsa21321")

   // WORKS
     const user1 = await CartModel.find({userId:"asdasdsa21321"})

Any ideas?

The issue was that I was using invalid key.

return await this.find({itemId: userId});

so I replaced it with

return await this.find({userId: userId});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM