let newdata = await tripModel.find(
{ tripId: ID },
{salesOrder:1, no: "$deliveryDetails.invoiceNo", _id: 0 }
);
let nullInvoices = [];
console.log("vvvvvvv", newdata[0].salesOrder);
for (let i = 0; i < newdata[0].no.length; i++) {
if (newdata[0].no[i] === null)
nullInvoices.push({
SO: newdata[0].salesOrder[i],
invoice: newdata[0].no[i],
});
}`
//error: vvvvvvv {
salesOrder: [
623d917d4c9f21730ace6256,
623da8bbeae48b2f34372a95,
623da8bbeae48b2f34372a92,
623d98964c9f21730ace642c
],
no: [ null, ‘0900009293’, ‘0900009294’, null ] }
reason TypeError: Cannot read properties of undefined (reading ‘length’)I need to access "no", but it is throwing as undefined.
2
Answers
Since your
console.log("vvvvvvv", newdata[0].salesOrder);
statement returnsyour iteration must be like as below
Sample Implementation
Actually, you can make an
invoices
object first, and then filter it by any condition: