简体   繁体   English

数组内部的 Object 无法更新

[英]Object inside array impossible to update

I have my model in Mongoose which inside has an array of objects.我在 Mongoose 中有我的 model,里面有一个对象数组。 Inside this array I have an object called disabledBy to which I want to set data but Mongoose always tells me that it does not receive information for said object.在这个数组中,我有一个名为 disabledBy 的 object,我想为其设置数据,但 ZCCADCDEDB567ABAE643E15DCF0974E503Z 总是告诉我它没有收到关于 object 的信息。

This is my Model:这是我的 Model:

Purchases购买

... ...

    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']
            }
        }
    }],

And this is my code from Express:这是我来自 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 });

It should be noted that when trying to update the status property of payments, it is executed correctly but not disabledBy.需要注意的是,当尝试更新支付的状态属性时,它是正确执行的,但不是 disabledBy。

This is the error reported by Mongoose:这是ZCCADCDEDB567ABAE643E15DCF0974E503Z报告的错误:

error Purchase validation failed: payments.0.disabledBy.comments: El Comentario es obligatorio, payments.0.disabledBy.username: Path disabledBy.username is required., payments.0.disabledBy.uid: Path disabledBy.uid is required.错误购买验证失败:payments.0.disabledBy.comments:El Comentario es obligatorio,payments.0.disabledBy.username:路径disabledBy.username是必需的,payments.0.disabledBy.uid:路径disabledBy.uid是必需的。

Even though the information I want to set in disabledBy is correct, I always get the Mongoose error.即使我想在 disabledBy 中设置的信息是正确的,我总是会收到 Mongoose 错误。 How can I set that information?我该如何设置该信息? Thanks.谢谢。

It's very strange but it only works if I remove the property from being required.这很奇怪,但只有当我从需要的属性中删除时它才有效。 Example of object without required working fine:无需正常工作的 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