我正在尝试在heroku上部署我的工作,解决了一些错误之后,一个错误并没有得到解决,我尝试了很多方法,但都失败了
添加了:useNewUrlParser: true,useCreateIndex: true, app.use(express.urlencoded({extended: false}))
const express = require('express');
const passport = require('passport')
const cors = require('cors');
const app = express();
const hostname='localhost';
var port= process.env.PORT || 8080;
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const route=require('./routes/api/Routes');
// DB Config
const db = require('./config/keys').mongoURI
// Connect to mongo
mongoose
.connect(db,{ useNewUrlParser: true,useCreateIndex: true, })
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.log(err))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.json())
app.use(express.urlencoded({extended: false}))
app.use(cors())
app.get('/test', (req,res) => res.send(`<h1>Deployed on Heroku</h1>`))
并且git bash中的错误是
State changed from starting to crashed
/app/node_modules/mongoose/lib/connection.js:434
throw new MongooseError('The `uri` parameter to `openUri()` must be a '
+
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.
https://teamse7.herokuapp.com/test上的显示应用程序错误
答案 0 :(得分:2)
这就是为什么通过添加以下行将节点版本添加到package.json中很重要的原因:
"engines": { "node": "your-version" }
同样,您应该使用Heroku仪表板(最简单的方法),在“设置”标签下,然后在配置变量中声明其他环境变量。
答案 1 :(得分:1)
在我的情况下,我遇到了同样的问题,这是因为我在本地设置了环境变量,但我不知道我们也必须在 heroku 中设置它们。 查看这篇文章了解更多信息 heroku official blog on config vars
答案 2 :(得分:0)
我将连接更改为
mongoose.connect('mongodb+srv://*********@********.mongodb.net/test?
retryWrites=true', {useNewUrlParser: true});
mongoose.connection.once('open', function(){
console.log('Conection has been made!');
}).on('error', function(error){
console.log('Error is: ', error);
});