收到错误后,我只是复制了实验室代码,因此不应该有代码端错误.
在我的. dev文件中,我有:
MONGODB_URI=mongodb+srv://<user>:<password>@phase-1-db.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000
我在Azure Cosmos DB for MongoDB(vCore)概述部分中用我的管理员用户名替换了用户
我将密码替换为我用管理员收件箱设置的密码.
我用来连接的index.js中的代码是
require('dotenv').config();
const { MongoClient } = require('mongodb');
async function main() {
// initialize the MongoDB client
const client = new MongoClient(process.env.MONGODB_URI);
// connects to the database service and outputs messages to the console to indicate the connection status.
try {
await client.connect();
console.log('Connected to MongoDB');
} catch (err) {
console.error(err);
} finally {
await client.close();
console.log('Disconnected from MongoDB');
}
}
main().catch(console.error);
我已经复制了Package.json并运行了nPM安装
{
"name": "mongodb-nodejs-devguide-first-cosmos-db-application",
"version": "1.0.0",
"description": "A Node.js application that connects to MongoDB",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"mongodb": "6.3.0",
"dotenv": "16.4.4"
},
"author": "Microsoft",
"license": "MIT"
}
然后我运行nPM start,收到这个错误:
npm start
> mongodb-nodejs-devguide-first-cosmos-db-application@1.0.0 start
> node index.js
MongoServerSelectionError: Server selection timed out after 30000 ms
at EventTarget.<anonymous> (/Users/user1/Desktop/github/Azure-OpenAI-Tutorials/first_cosmos_db_application/NodeJS/node_modules/mongodb/lib/sdam/topology.js:276:34)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
at EventTarget.dispatchEvent (node:internal/event_target:757:26)
at abortSignal (node:internal/abort_controller:374:10)
at TimeoutController.abort (node:internal/abort_controller:396:5)
at Timeout.<anonymous> (/Users/user1/Desktop/github/Azure-OpenAI-Tutorials/first_cosmos_db_application/NodeJS/node_modules/mongodb/lib/utils.js:1011:92)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'c.phase-1-db.mongocluster.cosmos.azure.com:10260' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}
Disconnected from MongoDB
see descriptions