gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:02:09 24 4
gpt4 key购买 nike

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

本教程的完成存储库位于:https://github.com/Azure-Samples/react-cosmosdb但到目前为止,我只是在第 5 部分中实际尝试连接到 Cosmos 数据库。

我遇到了 Mongoose 版本的问题,并用这里找到的代码替换了 repo 代码: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://learn.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 。但我也没有“连接成功”。

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

有人知道这是怎么回事吗?我可能用错了 promise 吗? 任何帮助,将不胜感激!

最佳答案

对于任何可能遇到类似问题的人,我终于弄清楚了!问题出在我工作的防火墙上 - 它阻止了我与 CosmosDB 的连接。当我从家庭互联网上尝试时,效果很好。

关于mongodb - 使用 MongoDB API 连接到 CosmosDB 时出现超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48994974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com