将Google App Engine上的Mongoose连接到mlab

问题描述 投票:1回答:1

我无法连接到我的mlab mongodb,因为我部署在gcp app engine上的应用程序。应用程序在部署到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.

我已经检查了googles文档,并且可以找到任何用于mongoose连接,只有一个用于使用plain nodejs进行直接连接。提前致谢

node.js mongodb google-app-engine mongoose google-cloud-platform
1个回答
1
投票

我不确切地知道问题的确切位置,但不知何故,当我使用nconf包从json文件中读取数据库连接详细信息时,由google https://cloud.google.com/community/tutorials/nodejs-mongodb-on-appengine描述没有mongoose我能够解决它。我最初是从env变量中读取的。

`         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});`
© www.soinside.com 2019 - 2024. All rights reserved.