我无法连接到我在 gcp 应用引擎上部署的应用程序的 mlab mongodb。该应用程序在部署到 heroku 和 localhost 时工作正常。我已经检查了类似的线程,如 Connecting Mongoose on Google App Engine但仍然找不到解决办法。
(node:531) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): MongoNetworkError: failed to connect to
server[ds111422.mlab.com:11422] on firstconnect [MongoNetworkError:
connection 0 to ds111422.mlab.com:11422 timed out] (node:531) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In the
future, promise rejections that are not handled will terminate the Node.js
process with a non-zero exit code.
我检查了谷歌文档,可以找到任何 Mongoose 连接,只有一个使用普通的nodejs直接连接。 提前致谢
最佳答案
我不知道问题到底出在哪里,但不知何故,当我使用 nconf 包从 json 文件中读取数据库连接详细信息时,如 google https://cloud.google.com/community/tutorials/nodejs-mongodb-on-appengine 所描述的那样没有 Mongoose 我能够解决它。最初我是从环境变量中读取的。
` nconf = require('nconf');
const mongoose = require('mongoose');
//key.json contains mongodb connection information
nconf.argv().env().file('keys.json');
const user = nconf.get('mongoUser');
const pass = nconf.get('mongoPass');
const host = nconf.get('mongoHost');
const port = nconf.get('mongoPort')
const mongoDatabase=nconf.get('mongoDatabase');
let mongo_url=`mongodb://${user}:${pass}@${host}:${port}/${mongoDatabase}`;
mongoose.connect(mongo_url,{useNewUrlParser: true});`
关于node.js - 将 Google App Engine 上的 Mongoose 连接到 mlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52312214/