admin管理员组

文章数量:1312799

I am trying to deploy my work on heroku and after solving some errors, one error is not getting fixed and i tried so many methods but all are failing

added : 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>`))

and the error in git bash is

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.

on shows application error

I am trying to deploy my work on heroku and after solving some errors, one error is not getting fixed and i tried so many methods but all are failing

added : 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>`))

and the error in git bash is

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.

on https://teamse7.herokuapp./test shows application error

Share Improve this question asked Apr 5, 2019 at 17:37 Mohamed NasserMohamed Nasser 511 silver badge4 bronze badges 9
  • What is the return of require('./config/keys').mongoURI ? – FrV Commented Apr 5, 2019 at 17:41
  • @FrV if(process.env.NODE_ENV === 'production') module.exports = require('./keys_prod') else module.exports = require('./keys_dev') – Mohamed Nasser Commented Apr 5, 2019 at 17:45
  • module.exports = { mongoURI: process.env.MONGO_URI, } @FrV this is keys_prod – Mohamed Nasser Commented Apr 5, 2019 at 17:46
  • module.exports = { mongoURI: 'mongodb+srv://******:*******@******.mongodb/test?retryWrites=true', } @Frv this is keys_dev – Mohamed Nasser Commented Apr 5, 2019 at 17:47
  • Ok, try console.log(db), if is not a string, that's why it fails – FrV Commented Apr 5, 2019 at 17:47
 |  Show 4 more ments

3 Answers 3

Reset to default 4

That’s why it is important to add to package.json your node version by adding this line: "engines": { "node": "your-version" } Too, you should declare your other environment variables with Heroku dashboard (that’s the easiest way), under the “settings” tab, then Config Var.

i had faced same issue in my case it was because of environment variable i was setting it up locally but i didn't knew that we have to set them in heroku as well. check out this article for more info heroku official blog on config vars

I changed the connection to
mongoose.connect('mongodb+srv://*********@********.mongodb/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); });

本文标签: