Cannot read properties of undefined (reading 'type')

I am getting an error similar to TypeError: Cannot read properties of undefined (reading 'startsWith') with a different variable but same nonetheless. It is intermittent however so hard to debug. Mine is as follows:

Cannot read properties of undefined (reading 'type')

It is triggered sometimes from this code:

  await mongoose.connect(keys.MONGO_URI_ANALYSIS, {
      useUnifiedTopology: true,
      useNewUrlParser: true,
      useFindAndModify: false
  })