0

I'm currently trying to run my mongodb cluster, but everytime I run node index.js which is meant to initialize the connection, I get an error about an undefined url. I logged the error and it appears that the url's being passed initially appear as undefined, which I'm not sure why. I have attached screenshots of my index.js and db.js files. Index.js holds all the initialization code and db.js holds the function to set up the connection.

Index.js Index.js

db.js part one db.js part one

db.js part two db.js part two

My terminal results are as follows:

undefined in db.js
undefined in db.js
mongodb+srv://pennbuzz23:buzzingaway23@pennbuzz.ysazyjr.mongodb.net/?retryWrites=true&w=majority in index.js
mongodb+srv://pennbuzz23:buzzingaway23@pennbuzz.ysazyjr.mongodb.net/?retryWrites=true&w=majority in db.js
TypeError: Cannot read properties of undefined (reading 'startsWith')
    at connectionStringHasValidScheme (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb-connection-string-url/lib/index.js:9:30)
    at new ConnectionString (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb-connection-string-url/lib/index.js:85:34)
    at parseOptions (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb/lib/connection_string.js:191:17)
    at new MongoClient (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb/lib/mongo_client.js:46:63)
    at MongoClient.connect (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb/lib/mongo_client.js:252:24)
    at connect (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/db.js:9:46)
    at Object.getDB (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/db.js:35:15)
    at Object.<anonymous> (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/routes/DirectMessagingRoutes.js:1:35)
    at Module._compile (node:internal/modules/cjs/loader:1275:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
TypeError: Cannot read properties of undefined (reading 'startsWith')
    at connectionStringHasValidScheme (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb-connection-string-url/lib/index.js:9:30)
    at new ConnectionString (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb-connection-string-url/lib/index.js:85:34)
    at parseOptions (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb/lib/connection_string.js:191:17)
    at new MongoClient (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb/lib/mongo_client.js:46:63)
    at MongoClient.connect (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/node_modules/mongodb/lib/mongo_client.js:252:24)
    at connect (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/db.js:9:46)
    at Object.getDB (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/db.js:35:15)
    at Object.<anonymous> (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/Login-RegisterDBOperations.js:1:28)
    at Module._compile (node:internal/modules/cjs/loader:1275:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/db.js:16
       throw Error('Could not connect to the db');
             ^

Error: Could not connect to the db
    at connect (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/db.js:16:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.getDB (/Users/leonelcontreras/Documents/GitHub/project-pennbuzz/server/model/db.js:35:9) (If you see the first two results at the top, they are undefined url strings)

I attempted to rewrite the code to see if it was a function error or something of the sort, but nothing has been successful.

user7247147
  • 1,045
  • 1
  • 10
  • 24
  • Did you disclose your credentials? – Wernfried Domscheit Apr 18 '23 at 19:08
  • 1
    Please don't paste screenshots, use formatted text. See https://meta.stackoverflow.com/q/285551 – Wernfried Domscheit Apr 18 '23 at 19:08
  • Those code images don't look so good on this screen. Guessing from the text in the question, the function in db.js is being called somewhere else before it is called from index.js. You might try to have the db.js log its caller, see https://stackoverflow.com/q/280389/2282634 – Joe Apr 18 '23 at 19:55

0 Answers0