问题描述
当我尝试将我的应用程序(nodejs)连接到mongodb时出现此错误.非常感谢您的帮助.
I'm getting this error when I try to connect my app(nodejs) to mongodb. I really appreciate your help here.
MongoParseError: URI does not have hostname, domain name and tld
at parseSrvConnectionString (E:\Projects\NodeAPI\node_modules\mongodb-core\lib\uri_parser.js:41:21)
at parseConnectionString (E:\Projects\NodeAPI\node_modules\mongodb-core\lib\uri_parser.js:509:12)
at connect (E:\Projects\NodeAPI\node_modules\mongodb\lib\operations\mongo_client_ops.js:195:3)
at connectOp (E:\Projects\NodeAPI\node_modules\mongodb\lib\operations\mongo_client_ops.js:284:3)
at executeOperation (E:\Projects\NodeAPI\node_modules\mongodb\lib\utils.js:416:24)
at MongoClient.connect (E:\Projects\NodeAPI\node_modules\mongodb\lib\mongo_client.js:175:10)
at Function.MongoClient.connect (E:\Projects\NodeAPI\node_modules\mongodb\lib\mongo_client.js:341:22)
at Object.<anonymous> (E:\Projects\NodeAPI\server.js:12:13)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
name: 'MongoParseError',
[Symbol(mongoErrorContextSymbol)]: {} }
我的代码:
db.js
module.exports = {
uri : "mongodb+srv://mithun:*******@cluster0-s089x.mongodb.net/test?retryWrites=true"}
==================================================================
==================================================================
node_route.js
node_route.js
module.exports = function(app, db){
app.post('/notes', (req, res) => {
const note = {text: req.body.body, title: req.body.title};
db.collection('notes').insert(note, (err, results) => {
if(err){
res.send({'error': 'An error has occured'});
} else {
res.send(result.ops[0]);
}
});
});
};
========================================================================index.js
=======================================================================index.js
const noteRoutes = require('./note_route');
module.exports = function(app, db){
noteRoutes(app, db);
}
=========================================================================server.js
=========================================================================server.js
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const db = require('./config/db');
const app = express();
const port = 8000;
app.use(bodyParser.urlencoded({extended: true}));
MongoClient.connect(db.uri,{ useNewUrlParser: true }, (err, database) =>
{
if (err) return console.log(err);
require('./app/routes')(app, database);
app.listen(port, () => {
console.log("We are live on " +port);
});
});
==========================================================================
===========================================================================
module.exports = {
uri : "mongodb+srv://mithun:m3Thun#[email protected]/test?retryWrites=true&ssl=false"
}
我尝试使用ssl = false,但错误仍然相同.
I've tried with ssl= false but the error remains same.
推荐答案
我也遇到了这个问题,在尝试创建演示应用程序时,我使用自动生成密码更新了密码,即单击了自动生成密码并将其粘贴在连接字符串中,它对我有用.
I have also faced this issue, while trying to create a demo app, I updated my password using autogenerate password, i.e I clicked on auto generate password and pasted it in connection string, it worked for me.
这篇关于MongoParseError:URI没有主机名,域名和顶级域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!