nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
528 stars 118 forks source link

Prop alias always returns undefined #1288

Closed audriuiv closed 2 years ago

audriuiv commented 2 years ago

Is there an existing issue for this?

Current behavior

Aliases in schemas don't work.

Mongo document ` { "_id" : ObjectId("5bd6ff5ea392f7302f26c906"), "limit" : 3, "filter" : { "where" : [ [ { "column" : "is_disabled", "operator" : "$eq", "value" : "0", "is_eval" : false } ] ], "by" : [ [ "AccountStatistic", "item_amount", "DESC" ] ], "limit" : 0 }, "architecture" : [ { "select" : [ [ { "column" : "item_amount", "operator" : ">=", "value" : 3 } ] ], "items" : { "filter" : { "where" : [ [ { "column" : "created_at", "operator" : "gte", "value" : "Sequelize.literal('DATE_SUB(CURDATE(), INTERVAL 30 DAY)')", "is_eval" : true } ] ], "by" : [ [ "all_reads", "DESC" ] ], "limit" : 1 } }, "assets" : [ { "type" : "column", "assets" : [ { "tag" : "account_img", "style" : { "margin" : "0" }, "link" : { "is_show" : true, "second_slug" : "" }, "assets" : [ ] }, { "style" : {

                        },
                        "type" : "row",
                        "assets" : [
                            {
                                "tag" : "account_title",
                                "style" : {

                                },
                                "link" : {
                                    "is_show" : true,
                                    "second_slug" : ""
                                },
                                "assets" : [ ]
                            },
                            {
                                "tag" : "articles_amount",
                                "style" : {

                                },
                                "assets" : [ ]
                            }
                        ]
                    }
                ]
            },
            {
                "type" : "column",
                "assets" : [
                    {
                        "style" : {

                        },
                        "type" : "column",
                        "assets" : [
                            {
                                "tag" : "article_title",
                                "style" : {

                                },
                                "link" : {
                                    "is_show" : null,
                                    "second_slug" : ""
                                },
                                "assets" : [ ]
                            },
                            {
                                "tag" : "article_author",
                                "style" : {

                                },
                                "assets" : [ ]
                            },
                            {
                                "style" : {

                                },
                                "type" : "row",
                                "assets" : [
                                    {
                                        "tag" : "article_cr_date",
                                        "style" : {

                                        },
                                        "assets" : [ ]
                                    },
                                    {
                                        "tag" : "article_likes",
                                        "style" : {

                                        },
                                        "assets" : [ ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "select" : [
            [ ]
        ],
        "items" : null,
        "assets" : [
            {
                "type" : "row",
                "assets" : [
                    {
                        "tag" : "account_img",
                        "style" : {
                            "margin" : "0 5px 0 0"
                        },
                        "link" : {
                            "is_show" : true,
                            "second_slug" : ""
                        },
                        "assets" : [ ]
                    },
                    {
                        "style" : {

                        },
                        "type" : "column",
                        "assets" : [
                            {
                                "tag" : "account_title",
                                "style" : {

                                },
                                "link" : {
                                    "is_show" : true,
                                    "second_slug" : ""
                                },
                                "assets" : [ ]
                            },
                            {
                                "tag" : "articles_amount",
                                "style" : {

                                },
                                "assets" : [ ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
],
"position" : "pos_1"

} `

Minimized Schema only for two fields:

` @Schema({ collection: 'accounts_slideshow' }) export class AccountSlideshow extends Document { @Prop({ alias: 'limit', type: Number}) limit2: number

//@Prop({ type: SchemaFactory.createForClass(Filter), ref: () => Filter }) // filter2: Filter

// @Prop({ type: [ArchitectureItemSchema] }) // architecture: ArchitectureItem[]

@Prop({ alias: 'position' }) position2: string } export type AccountSlideshowDocument = AccountSlideshow & Document

export const AccountSlideshowSchema = SchemaFactory.createForClass(AccountSlideshow) `

Repository:

` import { Injectable } from '@nestjs/common' import { InjectModel } from '@nestjs/mongoose' import { Model } from 'mongoose' import { AccountSlideshow, AccountSlideshowDocument, } from 'schemas/account.slideshow'

@Injectable() export class AccountSlideshowsRepository { constructor( @InjectModel(AccountSlideshow.name) private readonly _slideshowModel: Model, ) {}

public async find(): Promise { const res = this._slideshowModel.findOne() return await res.exec() } } `

Nestjs module: @Module({ imports: [ MongooseModule.forFeature([ { name: AccountSlideshow.name, schema: AccountSlideshowSchema }, ]), SequelizeModule.forFeature([Account, Item]), ], providers: [ AccountSlideshowsRepository, AccountsRepository, AccountSlideService, AccountHandler, ItemsRepository, ], controllers: [AccountSlideController], }) export class AccountSlideModule {}

Service

` @Injectable() export class AccountSlideService { constructor( private readonly _slidesRepository: AccountSlideshowsRepository, private readonly _accountHandler: AccountHandler, ) {}

public async getAccounts(offset: number): Promise { const settings = await this._slidesRepository.find() console.log(settings) // works and gets plain JavaScript object from Mongo db console.log(settings.limit2) - undefined console.log(settings.position2) - undefined } } `

I get data from Mongo db. There is not errors. But aliases don't work. I know, this is a bug.

Minimum reproduction code

https://github.com

Steps to reproduce

No response

Expected behavior

console.log(settings.limit2) // --> 3

Package version

8.1.3

mongoose version

6.0.9

NestJS version

8.1.3

Node.js version

14.17.6

In which operating systems have you tested?

Other

No response

jmcdo29 commented 2 years ago

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

kamilmysliwiec commented 2 years ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.