繁体   English   中英

连接 MongoDb Atlas 服务器时出错

[英]Error on connecting to MongoDb Atlas Server

所以我目前正在编写一个 web 应用程序,我需要数据库,所以我决定使用 go 与 mongodb 和 Z72280D51F351CE141CFCBF. 到目前为止,我在 localhost 上测试了所有内容并且它有效,但我想将数据移动到服务器。

我想通过 node.js 应用程序连接到集群。

mongoose.connect(`mongodb+srv://${process.env.MONGO_ADMIN}:${process.env.MONGO_PASSWORD}
@secret-cluster.thdxy.mongodb.net/userDB?retryWrites=true&w=majority`,
{useNewUrlParser:true, useUnifiedTopology: true,useCreateIndex: true});

那是错误信息:

(node:25236) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
    at parseConnectionString (W:\web development udemy\Secrets\node_modules\mongodb\lib\core\uri_parser.js:565:21)
    at connect (W:\web development udemy\Secrets\node_modules\mongodb\lib\operations\connect.js:282:3)
    at W:\web development udemy\Secrets\node_modules\mongodb\lib\mongo_client.js:223:5
    at maybePromise (W:\web development udemy\Secrets\node_modules\mongodb\lib\utils.js:662:3)
    at MongoClient.connect (W:\web development udemy\Secrets\node_modules\mongodb\lib\mongo_client.js:219:10)   
    at W:\web development udemy\Secrets\node_modules\mongoose\lib\connection.js:788:12
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (W:\web development udemy\Secrets\node_modules\mongoose\lib\connection.js:785:19)
    at Mongoose.connect (W:\web development udemy\Secrets\node_modules\mongoose\lib\index.js:339:15)
    at Object.<anonymous> (W:\web development udemy\Secrets\app.js:28:10)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
(node:25236) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:25236) [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.

我建议您在整个代码中使用这个 char -> ( ' )来包围字符串值,而不是这个 -> ( ` )。

使用不止一种类型的代码可能会导致代码中出现一些错误,就像我在我的代码中注意到的那样。

或者简单地使用双标记 -> ( " ) 来防止由于它们的相似性而导致的任何错误。

无论如何,我在使用node.jsmongodb 版本 4.4.2的控制台中遇到了同样的错误问题,直到我注意到我使用的是'+srv//'而不是'+srv://',所以如果我可以给你任何建议:你可以通过重新组织代码来解决很多错误 所以,你可以解决你的问题重写(或复制和粘贴)你的代码如下,因为这可以防止任何错误由于字符错误或东西,就像我做的那样:

// --- creating data for link and functions--- 

const port = 5000;
const myUser = 'userString';
const myPassword = 'passString';
const dataBaseName = 'yourDatabase';
const clusterInfo = 'yourClusterInfoGoesHere';

//----- creating url link ------ //CHECK YOUR MONGO VERSION FOR DETAILS ABOUT URL, mine is 4
// you can check your mongo version my tipying the line 'mongo -version' in your cmd

const url_databaseAcess =
'mongodb+srv://'
+myUser
+':'
+myPassword
+'@'
+clusterInfo
+'.mongodb.net/'
+dataBaseName
+'?retryWrites=true&w=majority';

// ----- handling connection ------

var db = mongoose.connection; //creating connection
db.on('error', console.error); //error log...
db.once('open', function() { console.log('sucess! connection created'); });
mongoose.connect(url_databaseAcess, { useNewUrlParser:true, useUnifiedTopology: true, useCreateIndex: true });
app.listen(port, function () {console.log('CONNECTED!!!'; });
mongoose.Promise = global.Promise;
const uri = `mongodb+srv://${process.env.MONGO_ADMIN}:${process.env.MONGO_PASSWORD}@secret-cluster.thdxy.mongodb.net/userDB?retryWrites=true&w=majority`;

mongoose.connect(uri, {
  useCreateIndex: true,
   useNewUrlParser:true
  }).then(()=> console.log('mongoDb conected..!!'))
   .catch(err=> console.log(err));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM