网址格式错误,无法解析

时间:2018-08-03 10:16:39

标签: node.js mongodb reactjs mlab mern

这是我捕获的错误:

captured

这是我的编码文件:

服务器文件

server file

密钥文件

keys file

我只是使用mongodb,node和reactjs创建到mlab的连接。我正在使用mongo db版本4。

需要您的帮助。

[nodemon] starting `node server.js`
Error: URL malformed, cannot be parsed
    at module.exports (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\url_parser.js:17:21)
    at deprecated (internal/util.js:47:15)
    at connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:179:3)
    at connectOp (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\operations\mongo_client_ops.js:283:3)
    at executeOperation (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\utils.js:420:24)
    at MongoClient.connect (C:\MERNapps\TestMERN08\node_modules\mongodb\lib\mongo_client.js:168:10)
    at Promise (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:493:12)
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\connection.js:490:19)
    at Mongoose.connect (C:\MERNapps\TestMERN08\node_modules\mongoose\lib\index.js:230:15)
    at Object.<anonymous> (C:\MERNapps\TestMERN08\server.js:12:2)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
(node:3696) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
server running on port 5000

2 个答案:

答案 0 :(得分:1)

问题出在您的钥匙上。尝试使用:

module.exports= {
  mongoURI : "your-uri-string",
  options:{key:value}
}

并使用以下键:

mongoose.connect(db.mongoURI,options);

答案 1 :(得分:1)

MongoDB地图集的一个非常常见的错误。最好还是遵循此程序

  1. 安装Mongo DB罗盘

  2. 使用默认设置

  

主机名:本地主机和端口27017

  1. 使用项目名称创建一个数据库,然后收集:user
  2. 在条目js上连接以下代码。路线
  

mongoose.connect('mongodb:// localhost / projectname')       .then(()=> console.log('mongo db working'))       .catch(err => console.log('ERROR is',err))