BETA
Disable Effect

CAD in the browser

Game Development in Go

Network Multimeter made from Raspberry Pi

CPP React and deprecating the observer model

This one has been bugging us for a while at work, so it seemed like a good one to blog about.

TypeError: Cannot read property 'raw' of undefined
    at [object Object].<anonymous> ([...]node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:436:77)
    at EventEmitter.emit (events.js:95:17)
    at [object Object].<anonymous> ([...]node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:201:13)
    at EventEmitter.emit (events.js:98:17)
    at Socket.<anonymous> ([...]node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:439:22)
    at EventEmitter.emit (events.js:95:17)[...]

Debugging this involved a bit of luck, particularly in finding a way to reproduce it outside our production environment, and then again in getting it to go away. That the error occurred in a sub-package outside the context of the main application made it difficult to tie back to a particular piece of our code.

The problem stemmed from the use of the MongoDB database driver in the main app, separately from Mongoose. Earlier work we did sought to establish a single database connection that could be shared between code using Mongoose and code using MongoDB directly. The way we did this was by establishing a connection through Mongoose, then passing the database client object out for use by the MongoDB driver.

  var mongooseDB = mongoose.connect(process.env.MONGO_URL, { db: { w: 'majority' }})
  var db = _.first(mongooseDB.connections).db

Elsewhere, we created Collections objects using db, with calls to the local MongoDB package, rather than the one inside Mongoose. That resulted in a consistency problem between the package used by Mongoose and the local package. When the database sent a response to a query, the handler for that connection couldn't be found by the driver that responded to it, hence the error.

The solution was to change require("mongodb") to require("mongoose/node_modules/mongodb") everywhere that we included it. That gave us a single, shared instance of the MongoDB driver and the errors stopped.

Lazy JS versus underscore and lodash