【发布时间】:2020-02-03 00:33:39
【问题描述】:
我是编程新手,我正在关注“getting MEAN”一书。不幸的是,关于 MONGODB 和 HEROKU 的部分使用了 MLAB,我想使用 AtlasMongoDB。 所以,我连接数据库的代码是
const mongoose = require ('mongoose');
let dbURI = 'mongodb://localhost/Loc8r';
if (process.env.NODE_ENV === 'production') {dbURI = process.env.MONGODB_URI;
}
mongoose.connect(dbURI, { useNewUrlParser: true, useCreateIndex: true});
当我在本地启动我的代码并声明 PROCESS.ENV.NODE_ENV 和 MONGODB_URI 时,应用程序可以很好地连接到数据库(代码如下)
NODE_ENV=production MONGODB_URI='mongodb+srv://username:password@cluster0-jbcwd.azure.mongodb.net/Loc8r?retryWrites=true&w=majority' nodemon
不过,当我将应用程序部署到 Heroku 时,我收到了以下错误
2019-10-03T21:34:53.253779+00:00 app[web.1]: /app/node_modules/mongoose/lib/connection.js:520
2019-10-03T21:34:53.253781+00:00 app[web.1]: throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
2019-10-03T21:34:53.253783+00:00 app[web.1]: ^
2019-10-03T21:34:53.257066+00:00 app[web.1]: Error [MongooseError]: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
我在 Heroku 中声明了这两个变量(node_env 等于生产,mongodb_uri 等于 mongodb+srv://username:password@cluster0-jbcwd.azure.mongodb.net/Loc8r?retryWrites=true)
你能帮帮我吗? 我意识到在没有任何引号的情况下启动 nodemon 声明 MONGODB_URI 也会产生错误(如下)。
NODE_ENV=production MONGODB_URI=mongodb+srv://username:password@cluster0-jbcwd.azure.mongodb.net/Loc8r?retryWrites=true&w=majority nodemon
可能是类似的东西导致了问题吗? 我知道我在这方面很糟糕,所以请不要吝啬
【问题讨论】:
标签: node.js mongodb heroku mongoose