Home > front end >  Looping over mongoose array results in undefined object
Looping over mongoose array results in undefined object

Time:07-23

Here's my code:

router.get("/bookings", async (req, res) => {
    var bookings = await Booking.find()
    console.log(bookings)
    result = []
    for (i = 0; i < bookings.length; i  ) {
        const challan = await Challan.findOne({ _id: bookings[i].challan });

        var packagesDelivered = 0;
        console.log('Booking', JSON.stringify(bookings[i]))
        console.log('GR NO', bookings[i].grNo)
        for (j = 0; j < bookings[i].deliveries.length; j  ) {
            const delivery = await Delivery.findOne({ _id: bookings[i].deliveries[j] })
            packagesDelivered  = delivery.noOfPackages;
        }
        

        result.push({ packagesDelivered: packagesDelivered, deliveries: bookings[i].deliveries, bookingDate: bookings[i].bookingDate, grNo: bookings[i].grNo, noOfPackages: bookings[i].noOfPackages, contents: bookings[i].contents, weight: bookings[i].weight, toPay: bookings[i].toPay, privateMark: bookings[i].privateMark, challanNo: challan.challanNo })

    }

    res.send(result)
})

I sometimes see console.log('GR NO', bookings[i].grNo) throwing an error that bookings[i] is undefined.

Booking undefined
        console.log('GR NO', bookings[i].grNo)
                                         ^

TypeError: Cannot read properties of undefined (reading 'grNo')
    at /Users/john/projects/myproj1/routes.js:396:36
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I am not understanding why it would be undefined since it's already been picked up by the for loop.

bookings:

[
  {
    _id: new ObjectId("62d6a1c8663cadb009367207"),
    bookingDate: '2022-07-17',
    grNo: '379757',
    noOfPackages: 40,
    contents: 'OLD CLOTHX',
    weight: 2000,
    toPay: 12000,
    privateMark: 'UNKWN',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d6a1c8663cadb009367205"),
    bookingDate: '2022-07-16',
    grNo: '379753',
    noOfPackages: 6,
    contents: 'GOODS',
    weight: 180,
    toPay: 1260,
    privateMark: 'SHIVAM',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d6a1c8663cadb009367203"),
    bookingDate: '2022-07-14',
    grNo: '389553',
    noOfPackages: 1,
    contents: 'HARDWARE GOODS',
    weight: 50,
    toPay: 350,
    privateMark: 'UNKWN',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d6a1c7663cadb009367201"),
    bookingDate: '2022-07-16',
    grNo: '387197',
    noOfPackages: 11,
    contents: 'E RICKSHAW',
    weight: 450,
    toPay: 3150,
    privateMark: 'UNKWN',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d6a1c7663cadb0093671ff"),
    bookingDate: '2022-07-17',
    grNo: '379756',
    noOfPackages: 18,
    contents: 'OLD CLOTH',
    weight: 1140,
    toPay: 6840,
    privateMark: 'DL ',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d6a1c7663cadb0093671fd"),
    bookingDate: '2022-07-16',
    grNo: '379754',
    noOfPackages: 12,
    contents: 'OLD CLOTH',
    weight: 880,
    toPay: 5280,
    privateMark: 'BB',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d6a1c7663cadb0093671fb"),
    bookingDate: '2022-07-17',
    grNo: '79352',
    noOfPackages: 116,
    contents: 'WASTE PAPER',
    weight: 6920,
    toPay: 31140,
    privateMark: 'UNKWN',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d6a1c7663cadb0093671f9"),
    bookingDate: '2022-07-16',
    grNo: '86520',
    noOfPackages: 35,
    contents: 'BED SHEET',
    weight: 2800,
    toPay: 15900,
    privateMark: 'UNKWN',
    deliveries: [],
    challan: new ObjectId("62d6a1c7663cadb0093671f7"),
    __v: 0
  },
  {
    _id: new ObjectId("62d69f6e663cadb0093670de"),
    bookingDate: '2022-07-16',
    grNo: '388755',
    noOfPackages: 7,
    contents: 'HARDWARE GOODS',
    weight: 330,
    toPay: 2310,
    privateMark: 'UNKWN',
    deliveries: [],
    challan: new ObjectId("62d69f6a663cadb0093670b6"),
    __v: 0
  },
  {
    _id: new ObjectId("62d69f6d663cadb0093670dc"),
    bookingDate: '2022-07-15',
    grNo: '380048',
    noOfPackages: 6,
    contents: 'MOTOR PARTS',
    weight: 300,
    toPay: 2100,
    privateMark: 'UNKWN',
    deliveries: [],
    challan: new ObjectId("62d69f6a663cadb0093670b6"),
    __v: 0
  }
]

CodePudding user response:

You forgot to declare result, i and j as local variables, therefore they are global and used by multiple parallel requests concurrently. Because of this, bookings[i] can suddenly mean something entirely different, and lead to the observed error.

var result = []
for (var i = 0; i < bookings.length; i  ) {
  ...
  for (var j = 0; j < bookings[i].deliveries.length; j  ) {
    ...
  • Related