【问题标题】:Connecting Mongoose on Google App Engine to mlab将 Google App Engine 上的 Mongoose 连接到 mlab
【发布时间】:2019-02-18 02:24:59
【问题描述】:

对于部署在 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.

我检查了谷歌的文档,可以找到任何用于猫鼬连接的,只有一个用于使用普通 nodejs 的直接连接。 提前致谢

【问题讨论】:

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


    【解决方案1】:

    我不知道问题出在哪里,但不知何故,当我使用谷歌https://cloud.google.com/community/tutorials/nodejs-mongodb-on-appengine 描述的 nconf 包从 json 文件中读取 db 连接详细信息时,没有猫鼬我能够解决它。最初我是从环境变量。

    `         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});`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2019-01-09
      相关资源
      最近更新 更多