Nestjs Mongoose prop alias always returns undefined

1k Views Asked by At

@nestjs/mongoose module. Mongo collection has different properties than Mongoose model. I use aliases, but they return undefined results.

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<AccountSlideshowDocument>,
  ) {}

  public async find(): Promise<AccountSlideshow> {
    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<SlideDto> {
    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. Why? I search solutions in github, but didn't find its. What I do wrong?

1

There are 1 best solutions below

0
On

Try to pass additional serialization options (toObject in your case):

@Schema({ collection: 'accounts_slideshow' }, {
    toObject: { virtuals: true },
    toJSON: { virtuals: true }
})