【问题标题】:Heroku logs error is Mongoose error uri parameter to openURI() must be a string and it is a stringHeroku 记录错误是 Mongoose 错误 uri 参数到 openURI() 必须是一个字符串,它是一个字符串
【发布时间】:2019-08-27 16:07:50
【问题描述】:

我正在尝试在 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 上显示应用程序错误

【问题讨论】:

  • require('./config/keys').mongoURI 的返回值是什么?
  • @FrV if(process.env.NODE_ENV === 'production') module.exports = require('./keys_prod') else module.exports = require('./keys_dev')
  • module.exports = { mongoURI: process.env.MONGO_URI, } @FrV 这是keys_prod
  • module.exports = { mongoURI: 'mongodb+srv://******:*******@******.mongodb.net/test? retryWrites=true', } @Frv 这是keys_dev
  • 好的,试试console.log(db),如果不是字符串,就是失败的原因

标签: javascript node.js express heroku mongoose


【解决方案1】:

我将连接更改为
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); });

【讨论】:

    【解决方案2】:

    这就是为什么通过添加以下行将您的节点版本添加到 package.json 很重要: "engines": { "node": "your-version" } 同样,您应该使用 Heroku 仪表板(这是最简单的方法)声明您的其他环境变量,在“设置”选项卡下,然后是 Config Var。

    【讨论】:

      【解决方案3】:

      在我的情况下,我遇到了同样的问题,这是因为我在本地设置环境变量,但我不知道我们也必须在 heroku 中设置它们。 查看这篇文章了解更多信息 heroku official blog on config vars

      【讨论】:

        猜你喜欢
        • 2023-02-11
        • 2021-05-21
        • 2019-10-05
        • 1970-01-01
        • 2023-03-31
        • 2021-06-11
        • 2020-03-06
        • 2019-08-11
        • 2020-12-01
        相关资源
        最近更新 更多