簡體   English   中英

數組內部的 Object 無法更新

[英]Object inside array impossible to update

我在 Mongoose 中有我的 model,里面有一個對象數組。 在這個數組中,我有一個名為 disabledBy 的 object,我想為其設置數據,但 ZCCADCDEDB567ABAE643E15DCF0974E503Z 總是告訴我它沒有收到關於 object 的信息。

這是我的 Model:

購買

...

    payments: [{
        createdAt: {
            type: String,
            required:true
        },
        paymentNumber: {
            type: Number,
            default:0
        },
        previousBalance:{
            type: Number,
            required: [true, 'El Saldo anterior es obligatorio'],
            default:0
        },
        paymentAmount:{
            type: Number,
            required: [true, 'La Cantidad del Pago(Abono) es obligatorio'],
            default:0
        },
        outstandingBalance:{
            type: Number,
            required: [true, 'El Saldo pendiente es obligatorio'],
            default:0
        },
        createdBy:{
            uid : { type: String, required: true },
            username:{ type: String, required: true }
        },
        comments: {
            type: String,
            maxlength:300,
        },
        status: {
            type: Boolean, 
            default: true
        },
        disabledBy:{ // this is the object i try to update
            uid:{
                type: String,
                required:true
            },
            username:{
                type: String,
                required:true
            },
            date:{
                type: Date,
                default: Date.now
            },
            comments: {
                type: String,
                maxlength:300,
                required: [true, 'El Comentario es obligatorio']
            }
        }
    }],

這是我來自 Express 的代碼:

       const payment = await Purchase.updateOne( 
        { _id: id, 'payments._id': body.paymentId },
        {
          $set: {
            'payments.$.status': false,
            'payments.$.disabledBy': {
                uid: req.user._id,
                username: req.user.username,   
                comments: body.comments
            }
          }
        } ,{ new: true });

需要注意的是,當嘗試更新支付的狀態屬性時,它是正確執行的,但不是 disabledBy。

這是ZCCADCDEDB567ABAE643E15DCF0974E503Z報告的錯誤:

錯誤購買驗證失敗:payments.0.disabledBy.comments:El Comentario es obligatorio,payments.0.disabledBy.username:路徑disabledBy.username是必需的,payments.0.disabledBy.uid:路徑disabledBy.uid是必需的。

即使我想在 disabledBy 中設置的信息是正確的,我總是會收到 Mongoose 錯誤。 我該如何設置該信息? 謝謝。

這很奇怪,但只有當我從需要的屬性中刪除時它才有效。 無需正常工作的 object 示例:

disabledBy:{
        uid:{
            type: String
        },
        username:{
            type: String
        },
        date:{
            type: Date,
            default: Date.now
        },
        comments: {
            type: String,
            maxlength:300
        }
    }

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM