【问题标题】:Mongoose Packs Each Object In An ArrayMongoose 将每个对象打包成一个数组
【发布时间】:2020-11-12 12:28:51
【问题描述】:

社区! 我遇到了一些关于猫鼬的问题。它将数组数组而不是对象数组添加到数据库中,并将每个对象打包到一个数组中。怎么了?

所以我创建了 PaymentMethod,将 Array<IPayment> 设置为 fee

export class PaymentMethod{
    @prop({ required: true, unique: true })
    public name: String

    @prop({ required: true })
    public details: String

    @prop({ required: true })
    public inlineMenu: String

    @prop({ required: true })
    public callbackData: String

    @prop({ required: true })
    public fee: Array<IPaymentFee>

    @prop({ default: false })
    public available: Boolean
} 

IPaymentFee接口:

export interface IPaymentFee {
    from: Number
    type: 'percent' | 'fixed'
    amount: Number
    extra?: Number | 0
}

之后我创建了一种新的付款方式:

const createdPayment = await PaymentMethodModel.create({
    name: 'xbank',
    details: '43284809328432',
    inlineMenu: 'menu_payment_xbank',
    callbackData: 'client_payment_xbank',
    available: true,
    fee: [
        { from: 0, type: 'fixed', amount: 200 },
        { from: 1000, type: 'percent', amount: 20, extra: 150 },
        { from: 1500, type: 'percent', amount: 17 },
        { from: 2000, type: 'percent', amount: 15 },
        { from: 3500, type: 'percent', amount: 13 },
        { from: 10000, type: 'percent', amount: 12 },
        { from: 20000, type: 'percent', amount: 11 },
        { from: 30000, type: 'percent', amount: 10 },
    ]}
)

但最终我用数组数组而不是对象数组得到了不正确的结果—— screenshot here

我做错了什么?提前谢谢!

【问题讨论】:

    标签: javascript mongodb typescript mongoose


    【解决方案1】:

    通过向所有 find/findOne 方法添加方法 .lean() 解决了这个问题 ?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      相关资源
      最近更新 更多