在heroku上部署mongodb地图集后,无法解决该错误?

时间:2019-09-02 10:20:28

标签: node.js mongodb heroku mongoose mongodb-atlas

在本地应用程序运行正常,但是当我将其部署在heroku上时,heroku日志显示错误。数据也没有添加到mongoDb数据库中。实际上,在应用程序中,我使用了google-oauth登录,因此在本地它可以完美运行,但在heroku中则不能。我还在mongodb地图集上将我的IP列入了白名单,但仍然不知道为什么会出现此错误。

任何人都可以帮助我....

这是我的prod.js文件

module.exports = {
    googleClientID: process.env.GOOGLE_CLIENT_ID,
    googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
    mongoURI: process.env.MONGO_URI,
    cookieKey: process.env.COOKIE_KEY
}

这是我的keys.js文件

// Keys.js - figure out what credentials should return

if (process.env.NODE_ENV === "production") {
    // So we are in prodution - return the prod set of keys
    module.exports = require('./prod');
} else {
    // We are developement - return the dev keys
    module.exports = require('./dev');
}

这是我的index.js文件

const express = require('express');
const mongoose = require('mongoose');
const passport = require('passport');
const cookieSession = require('cookie-session');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport');

const app = express();

//Middleware
app.use(
    cookieSession({
        maxAge: 30 * 24 * 60 * 60 * 1000,
        keys: [keys.cookieKey]
    })
);
app.use(passport.initialize());
app.use(passport.session());

// mongoDb Connection
mongoose.connect(keys.mongoURI, {
        useNewUrlParser: true
    }).then(() => console.log('MongoDb Connect successfully'))
    .catch(err => console.log(err));


// actual routes
require('./routes/authRoutes')(app);


const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server is running in ${PORT}`));

实际上我首先遇到两个不同的错误是

2019-09-02T06:28:59.760121+00:00 app[web.1]: Warning: connect.session() MemoryStore is not
2019-09-02T06:28:59.760149+00:00 app[web.1]: designed for a production environment, as it will leak
2019-09-02T06:28:59.760151+00:00 app[web.1]: memory, and will not scale past a single process.
2019-09-02T06:28:59.763577+00:00 app[web.1]: 
2019-09-02T06:28:59.763581+00:00 app[web.1]: /app/node_modules/mongoose/lib/connection.js:519
2019-09-02T06:28:59.763583+00:00 app[web.1]: throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
2019-09-02T06:28:59.763585+00:00 app[web.1]: ^
2019-09-02T06:28:59.763838+00:00 app[web.1]: 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.
2019-09-02T06:28:59.763842+00:00 app[web.1]: at new MongooseError (/app/node_modules/mongoose/lib/error/mongooseError.js:10:11)
2019-09-02T06:28:59.763844+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:519:11)
2019-09-02T06:28:59.763846+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:321:15)
2019-09-02T06:28:59.763848+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:24:10)
2019-09-02T06:28:59.763850+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:701:30)
2019-09-02T06:28:59.763852+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
2019-09-02T06:28:59.763854+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:600:32)

第二个是这个

2019-09-02T08:49:22.115492+00:00 app[web.1]: { MongoNetworkError: failed to connect to server [productionproj-shard-00-00-qfouv.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to productionproj-shard-00-00-qfouv.mongodb.net:27017 closed]

如此......同上

这是我在heroku config vars中添加的我的mongodb生产url

mongodb+srv://nansProdDeploy:nansProdDeploy@productionproj-qfouv.mongodb.net/test?retryWrites=true&w=majority

1 个答案:

答案 0 :(得分:0)

所有关于将正确的IP地址列入白名单。当我们将应用程序放置在heroku上时,我们必须通过单击 [允许从任何地方访问] 按钮将该IP列入白名单,然后单击ok,您的IP将为0.0.0.0/0,该IP将允许访问您部署的heroku应用。此时,当您在本地使用应用程序时,必须选择 [添加当前IP地址] 以在本地访问应用程序