在 mongoDB 中将多个对象推送到嵌套数组中

push multiple objects in nested array in mongoDB

我试图将多个对象压入一个子数组,但我不确定它是否正确。 (我认为这是不正确的,因为我有一个错误。)

TypeError: 无法读取未定义的属性(读取 'product_identifier');

console.log(req.body.delivery[0].toDeliver.product_identifier);

它给出未定义的并且浏览器中的 http 错误是 500。

这是我的后端代码。

export const delivery = async (req,res) => {

    const { id } = req.params;

    console.log(id);
    console.log(req.body.delivery[0]);

    try {
        if(!id) return res.status(404).json({ message: 'ID not found' });
        await OwnerModels.findByIdAndUpdate(id,
            {
                $push: {
                    delivery: 
                    {
                        clientname: req.body.delivery[0].clientname,
                        address: req.body.delivery[0].address,
                        email: req.body.delivery[0].email,
                        number: req.body.delivery[0].number,
                        toDeliver: [
                            {
                                product_identifier: req.body.delivery[0].toDeliver.product_identifier,
                                productname: req.body.delivery[0].toDeliver.productname,
                                price: req.body.delivery[0].toDeliver.price
                            }
                        ],
                        toDeliverPaidViaPaypal: []
                    }
                }
            },
            {
                new: true
            },(err,res)=> {
                if(err) return console.log(err);
                console.log(res);
            })
    } catch (error) {
        console.error(error);
        res.status(500).json({ message: 'Server error' });
    }
}

这是我的架构。

const OwnerSchema = mongoose.Schema({
    username: {
        require: true,
        type: String,
    },
    password: {
        require: true,
        type: String,
    },
    isAdmin: {
      type: Boolean,
      default: true,
    },
    store: [
        {
            product_identifier: {
              type: String,
              require: true,
            },
            productname: {
                type: String,
                required: true,
              },
            price: {
                type: Number,
                required: true,
              },
            quantity: {
                type: Number,
                required: true,
              },
            categoryfilter: {
              type: String,
              required: true
            },
            description: {
                type: String,
                required: true,
              },
            specs: {
              type: String,
              required: true
            },
            imageBase64: {
                type: String,
                required: true,
              },
            timestamp: {
              type: String,
              required: true,
            }
            
        }
    ],
    delivery: [
      {
        clientname: {
          type: String,
          required: true
        },
        address: {
          type: String,
          required: true
        }, 
        email: {
          type: String,
          required: true
        },
        number: {
          type: Number,
          required: true
        },
        toDeliver: [
          {
            product_identifier: {
              type: String,
              require: true,
            },
            productname: {
              type: String,
              required: true
            },
            price: {
              type: Number,
              required: true
            },
          }
        ],
        toDeliverPaidViaPaypal: [
          {
            product_identifier: {
              type: String,
              require: true,
            },
            productname: {
              type: String,
              required: true
            },
            price: {
              type: Number,
              required: true
            },
          }
        ]
      }
    ]
});

这是我的 Axios 请求。

export const delivery = (adminID,clientname,address,email,number,todeliver,viapaypal) => api.patch(`/checkout/${adminID}`, 
    {
        delivery: [
            {
                clientname: clientname,
                address: address,
                email: email,
                number: number,
                todeliver: todeliver, // array with 3 objects
                toDeliverPaidViaPaypal: viapaypal // empty array 
            }
        ]
    }
)

这是我要发送的数据。

{
  clientname: 'Gino Dela Vega',
  address: '008 Estrella.st Santo Cristo City of Malolos,Bulacan',
  email: 'gamexgaming1997@gmail.com',
  number: 9922325221,
  todeliver: [
    {
      product_identifier: 'tl9d3g1ut9pb2o13-87r0c',
      productname: 'DELL E2219HN',
      price: 7500,
      imageBase64: ........,
      quantity: '99',
      date: '1652347791630',
      _id: '627cd4d28c7976e1f02817a3'
    },
    {
      product_identifier: 'd173prauc2-t93ln1o6t58',
      productname: 'Hanns.G HL195',
      price: 4500,
      imageBase64: ........,
      quantity: '99',
      date: '1652347791630',
      _id: '627cd4d58c7976e1f02817aa'
    },
    {
      product_identifier: '87l2bu3o971tt1drcwp-3t',
      productname: 'LG E1941T',
      price: 4500,
      imageBase64: ........,
      quantity: '99',
      date: '1652347791630',
      _id: '627cd4da8c7976e1f02817b1'
    }
  ],
  toDeliverPaidViaPaypal: []

因为您的后端在 req.body.delivery[0].toDeliver 中调用 toDeliver 但在 axios 请求中您发送 todeliver。只需将 axios 请求从 todeliver 更改为 toDeliver 这里是:

delivery: [
            {
                clientname: clientname,
                address: address,
                email: email,
                number: number,
                toDeliver: todeliver, // <============ **HERE IS**
                toDeliverPaidViaPaypal: viapaypal // empty array 
            }
        ]