@nestjs/mongoose模块. Mongo集合与Mongoose模型有不同的属性,我使用了别名,但它们返回未定义的结果。
蒙哥文
{
"_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"
}
仅两个字段的最小化架构:
@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)
储存库:
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({
imports: [
MongooseModule.forFeature([
{ name: AccountSlideshow.name, schema: AccountSlideshowSchema },
]),
SequelizeModule.forFeature([Account, Item]),
],
providers: [
AccountSlideshowsRepository,
AccountsRepository,
AccountSlideService,
AccountHandler,
ItemsRepository,
],
controllers: [AccountSlideController],
})
export class AccountSlideModule {}
服务
@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
}
}
我从Mongo数据库获取数据。没有错误。但是别名不起作用。为什么?我在github中搜索解决方案,但是没有找到它。我做错了什么?
1条答案
按热度按时间v1uwarro1#
尝试传递其他序列化选项(在您的情况下为toObject):