node.js - 将 Google App Engine 上的 Mongoose 连接到 mlab

标签 node.js mongodb google-app-engine mongoose google-cloud-platform

我无法连接到我在 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/

相关文章:

node.js - Upstart 错误以状态 1 终止

linux - 如何在 Linux (Fedora-15) 上管理 Node.js 应用程序

node.js - 使用 Jest 进行测试失败,错误为 : Error watching file for changes: EMFILE

node.js - MariaDB 的 Sequelize 错误

mongodb - MongoDB 中的复合索引效率不明

json - MongoDB。如何更新数组内的json属性

MongoDB 高级查询——使用另一个字段的值

google-app-engine - 如何修复 "Invalid JSON payload received. Unknown name"错误

java - GAE - Javamail API 获取空内容邮件

python - 类和 Google App Engine