0

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
  }
]
1
  • One more thing I noticed here is that this only happens if the API is triggered more than once (while the first few are still being processed). Subsequent API hits results in this. Commented Jul 23, 2022 at 12:25

1 Answer 1

2

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++) {
    ...
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.