Vercel无法读取我的.env MongoDB连接字符串

mdfafbf1  于 2022-10-04  发布在  Go
关注(0)|答案(1)|浏览(241)

我在Vercel上部署了我的服务器ExpressJS+NodeJS。我已经完成了.env变量的设置,我还重新部署了我的服务器,但仍然收到这个错误

Vercel函数日志

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

我的.env变量在本地运行正常。它已经以“MongoDB+srv://

”开头

这是我的连接

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

这是它在我的Vercel

上显示的内容

kyvafyod

kyvafyod1#

根据注解中的确认,问题似乎是使用了不正确的变量名。如here所述,将其从DBHOST更改为MONGODB_URI可解决问题。

相关问题