使用MongoDB API连接到CosmosDB时出现超时错误

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

我目前正在通过以下教程学习如何通过mongodb API使用Azure cosmos db:qazxsw poi

本教程的完成回购在这里:https://docs.microsoft.com/en-us/azure/cosmos-db/tutorial-develop-mongodb-react但到目前为止,我只是在第5部分,你实际上尝试连接到cosmos数据库。

我遇到了一个mongoose版本的问题,用这里找到的repo代码替换了:https://github.com/Azure-Samples/react-cosmosdb

我还将我所需的mongodb版本降级为2.2.33,因为似乎较新版本的mongo不起作用。 (见底部的评论:Error connecting to Azure: Illegal character in password with mongoose 5.0.1 but works in 4.13.9

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

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]' }

下面是Azure中连接字符串页面的截图: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 };

这是我的package.json:

enter image description here

我在启动时也会收到以下错误:

{
  "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"
}

当我尝试刷新我期望看到一个空数组的页面时,我只看到一个chrome错误: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 并且控制台中显示以下内容:localhost didn’t send any data. ERR_EMPTY_RESPONSE并且我没有得到上面的大MongoError。但我也没有得到“连接成功”。

我认为这可能是我在连接网址中作为数据库名称放置的问题,所以我创建了一个名为heroes_test的集合并尝试使用GET /api/heroes - - ms - -,但同样的事情发生了。

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

mongodb azure mongoose azure-cosmosdb
1个回答
2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.