0

I'm trying to push multiple objects in a sub array but I'm not sure if it is correct. (I assume it's incorrect cause I have an error.)

TypeError: Cannot read properties of undefined (reading 'product_identifier');

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

it gives undefined and the http error in browser is 500.

here's my backend code.

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' });
    }
}

here's my schema.

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
            },
          }
        ]
      }
    ]
});

here's my Axios request.

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 
            }
        ]
    }
)

here's the data I'm trying to send.

{
  clientname: 'Gino Dela Vega',
  address: '008 Estrella.st Santo Cristo City of Malolos,Bulacan',
  email: '[email protected]',
  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: []

1 Answer 1

1

Because your backend you call toDeliver in req.body.delivery[0].toDeliver but in axios request you send todeliver. Just change in axios request from todeliver to toDeliver Here is:

delivery: [
            {
                clientname: clientname,
                address: address,
                email: email,
                number: number,
                toDeliver: todeliver, // <============ **HERE IS**
                toDeliverPaidViaPaypal: viapaypal // empty array 
            }
        ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.