mongodb-developer / mern-stack-example

Mern Stack code for the Mern Tutorial
Apache License 2.0
292 stars 261 forks source link

TypeError: Cannot read properties of undefined (reading 'collection') #27

Closed anedfung closed 1 year ago

anedfung commented 1 year ago

Hi, I'm pretty new to this. I cloned the repo, but I get this error as soon as the client app loads:

TypeError: Cannot read properties of undefined (reading 'collection') at C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\routes\record.js:19:6 at Layer.handle [as handle_request] (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\layer.js:95:5) at C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\index.js:174:3) at router (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\express\lib\router\index.js:47:12) MongoServerSelectionError: connection to 35.232.129.9:27017 closed at Timeout._onTimeout (C:\Users\anedf\Documents\GitHub\mern-stack-example\mern\server\node_modules\mongodb\lib\core\sdam\topology.js:439:30) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7) { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', setName: null, maxSetVersion: null, maxElectionId: null, servers: Map(3) { 'ac-0bptifw-shard-00-00.9kna9aa.mongodb.net:27017' => [ServerDescription], 'ac-0bptifw-shard-00-01.9kna9aa.mongodb.net:27017' => [ServerDescription], 'ac-0bptifw-shard-00-02.9kna9aa.mongodb.net:27017' => [ServerDescription] }, stale: false, compatible: true, compatibilityError: null, logicalSessionTimeoutMinutes: null, heartbeatFrequencyMS: 10000, localThresholdMS: 15, commonWireVersion: null } }

The only thing that I've changed is adding the config.env file. Does anyone know why this could be happening?

bsadov commented 1 year ago

Hey, I was having the same issue, it was due to mongodb v5 switching from callbacks to promises.

You have to make sure your server's mongodb dependency is the same version as the repo's (or at least below version 5). You could also install 'mongodb-legacy' and replace the references to mongodb in your server to use that instead. Or, though time consuming, you could update the server to use promises.

I used this as reference, https://github.com/mongodb/node-mongodb-native/blob/HEAD/etc/notes/CHANGES_5.0.0.md#changes