使用MongoDB API连接到CosmosDB时发生超时错误

时间:2018-02-26 18:38:50

标签: mongodb azure mongoose azure-cosmosdb

我目前正在通过以下教程学习如何通过mongodb API使用Azure的cosmosdb: https://docs.microsoft.com/en-us/azure/cosmos-db/tutorial-develop-mongodb-react

本教程完成的repo在这里:https://github.com/Azure-Samples/react-cosmosdb但到目前为止,我只是在第5部分,你实际上尝试连接到cosmos数据库。

我遇到了mongoose版本的问题,并用以下内容替换了回购代码:Error connecting to Azure: Illegal character in password with mongoose 5.0.1 but works in 4.13.9

我还将我所需的mongodb版本降级为2.2.33,因为似乎较新版本的mongo不起作用。 (参见底部的评论:https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-mongoose

这两个似乎解决了我的一些错误,但是我仍然坚持如何解决以下问题:

    { MongoError: failed to connect to server [react-cosmos-db-tori.documents.azure.com:10255] on first connect [MongoError: connection 0 to react-cosmos-db-tori.documents.azure.com:10255 timed out]
[1]     at Pool.<anonymous> (/Users/vhenri/Documents/dev/azure-app-service/azure-heros/server/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:336:35)
[1]     at emitOne (events.js:116:13)
[1]     at Pool.emit (events.js:211:7)
[1]     at Connection.<anonymous> (/Users/vhenri/Documents/dev/azure-app-service/azure-heros/server/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:280:12)
[1]     at Object.onceWrapper (events.js:317:30)
[1]     at emitTwo (events.js:126:13)
[1]     at Connection.emit (events.js:214:7)
[1]     at TLSSocket.<anonymous> (/Users/vhenri/Documents/dev/azure-app-service/azure-heros/server/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:199:10)
[1]     at Object.onceWrapper (events.js:313:30)
[1]     at emitNone (events.js:106:13)
[1]     at TLSSocket.emit (events.js:208:7)
[1]     at TLSSocket.Socket._onTimeout (net.js:407:8)
[1]     at ontimeout (timers.js:475:11)
[1]     at tryOnTimeout (timers.js:310:5)
[1]     at Timer.listOnTimeout (timers.js:270:5)
[1]   name: 'MongoError',
[1]   message: 'failed to connect to server [react-cosmos-db-tori.documents.azure.com:10255] on first connect [MongoError: connection 0 to react-cosmos-db-tori.documents.azure.com:10255 timed out]' }

据我所知,这只是暂停,因为它无法连接到我的数据库。

这是我的代码:

const mongoose = require('mongoose');
const env = require('./env/environment');

mongoose.Promise = global.Promise;

function connect() {
return(
    mongoose.connect('mongodb://react-cosmos-db-tori.documents.azure.com:10255/react-cosmos-db-tori?ssl=true', {
        auth: {
        user: 'react-cosmos-db-tori',
        password: '<mypasswordhere>'
        }
    })
    .then(() => console.log('connection successful'))
    .catch((err) => console.log(err))
 )
}
module.exports = {
  connect,
  mongoose
};

下面是Azure中我的连接字符串页面的屏幕截图: enter image description here

这是我的package.json:

{
  "name": "express-react-starter",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "body-parser": "^1.17.2",
    "cookie-parser": "^1.4.3",
    "debug": "~2.6.3",
    "express": "^4.15.3",
    "jade": "^1.11.0",
    "mongodb": "^2.2.33",
    "mongoose": "^5.0.7",
    "morgan": "^1.8.2",
    "react": "^15.6.1",
    "react-dom": "^15.6.1"
  },
  "devDependencies": {
    "concurrently": "^3.5.0",
    "nodemon": "^1.12.0",
    "react-scripts": "1.0.10"
  },
  "scripts": {
    "start": "concurrently \"react-scripts start\" \"nodemon server/server.js\"",
    "build": "react-scripts build && mv build server",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:3001"
}

我在开始时也遇到以下错误:

DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client

当我尝试刷新我希望看到空数组的页面时,我只看到一个chrome错误:localhost didn’t send any data. ERR_EMPTY_RESPONSE并且控制台中显示以下内容:GET /api/heroes - - ms - -我不喜欢上面没有大的MongoError。但我也没有得到“连接成功”。

我当时认为这可能是我在连接网址中作为数据库名称放置的问题,所以我创建了一个名为heroes_test的集合并尝试使用mongodb://react-cosmos-db-tori.documents.azure.com:10255/heroes_test?ssl=true,但同样的事情发生了。

任何人都知道这里发生了什么?我可能使用承诺错了吗?  任何帮助,将不胜感激!

1 个答案:

答案 0 :(得分:2)

对于任何可能偶然遇到这个类似问题的人,我终于明白了!问题出在我工作的防火墙上 - 它阻止了我与CosmosDB的连接。当我从我的家庭互联网上尝试它时,它工作正常。