Home > Software design >  Vercel cannot read my .env mongodb connection string
Vercel cannot read my .env mongodb connection string

Time:10-02

I deployed my server ExpressJS NodeJS on Vercel. I've done setting up my .env variables I also already redeployed my server but im still getting this error

Vercel function logs

d rejection: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb srv://"
    at new ConnectionString (/var/task/node_modules/mongodb-connection-string-url/lib/index.js:86:19)
    at parseOptions (/var/task/node_modules/mongodb/lib/connection_string.js:209:17)
    at new MongoClient (/var/task/node_modules/mongodb/lib/mongo_client.js:65:63)
    at /var/task/node_modules/mongoose/lib/connection.js:801:16
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/var/task/node_modules/mongoose/lib/connection.js:798:19)
    at /var/task/node_modules/mongoose/lib/index.js:381:10
    at /var/task/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/var/task/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
2022-10-01T12:01:22.392Z    undefined   WARN    Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
Legacy server listening...
2022-10-01T12:01:22.408Z    undefined   ERROR   Unhandled Promise Rejection     {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoParseError: Invalid scheme, expected connection string to start with \"mongodb://\" or \"mongodb srv://\"","reason":{"errorType":"MongoParseError","errorMessage":"Invalid scheme, expected connection string to start with \"mongodb://\" or \"mongodb srv://\"","stack":["MongoParseError: Invalid scheme, expected connection string to start with \"mongodb://\" or \"mongodb srv://\"","    at new ConnectionString (/var/task/node_modules/mongodb-connection-string-url/lib/index.js:86:19)","    at parseOptions (/var/task/node_modules/mongodb/lib/connection_string.js:209:17)","    at new MongoClient (/var/task/node_modules/mongodb/lib/mongo_client.js:65:63)","    at /var/task/node_modules/mongoose/lib/connection.js:801:16","    at new Promise (<anonymous>)","    at NativeConnection.Connection.openUri (/var/task/node_modules/mongoose/lib/connection.js:798:19)","    at /var/task/node_modules/mongoose/lib/index.js:381:10","    at /var/task/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5","    at new Promise (<anonymous>)","    at promiseOrCallback (/var/task/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: MongoParseError: Invalid scheme, expected connection string to start with \"mongodb://\" or \"mongodb srv://\"","    at process.<anonymous> (/var/runtime/index.js:35:15)","    at process.emit (events.js:412:35)","    at processPromiseRejections (internal/process/promises.js:245:33)","    at processTicksAndRejections (internal/process/task_queues.js:96:32)"]}
2022-10-01T12:01:22.413Z    undefined   ERROR   Unhandled rejection: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb srv://"
    at new ConnectionString (/var/task/node_modules/mongodb-connection-string-url/lib/index.js:86:19)
    at parseOptions (/var/task/node_modules/mongodb/lib/connection_string.js:209:17)
    at new MongoClient (/var/task/node_modules/mongodb/lib/mongo_client.js:65:63)
    at /var/task/node_modules/mongoose/lib/connection.js:801:16
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/var/task/node_modules/mongoose/lib/connection.js:798:19)
    at /var/task/node_modules/mongoose/lib/index.js:381:10
    at /var/task/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/var/task/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
RequestId: 15c29315-5ba5-44b4-87e0-60bac6bc3ffe Error: Runtime exited with error: exit status 1
Runtime.ExitError

My .env variable is working properly locally. It's already starting with "mongodb srv:// enter image description here

and here's my connection

mongoose.connect(process.env.DBHOST, 
    {
        useNewUrlParser: true,
    }
)

This is what it displays on my vercel enter image description here

CodePudding user response:

Per confirmation in the comments, the problem seems to have been the use of an incorrect variable name. Changing it from DBHOST to MONGODB_URI as mentioned here resolves the issue.

  • Related