UR分析器的折旧错误和连接错误,键值对不完整,mongo

时间:2018-11-07 21:03:25

标签: node.js mongodb express mongoose

我在Mongo上遇到了一个奇怪的问题,抛出了我似乎无法解决的错误。很奇怪,因为我的代码仍然有效。如果我达到终点,我就会得到数据,一切都很好,对吧?但是我还是宁愿没有挂在我身上的错误,也可能是一个问题。

我确实对SO上的这些错误进行了一些研究,特别是发现了Unhandled promise rejection: Error: URL malformed, cannot be parsedIncomplete key value pair for option remote mongodb,但是似乎没有可行的解决方案。

我的Mongo版本是4.0.2。我正在通过Mongoose v5.3.9进行连接。

这是错误:

(node:5305) 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.
connection error { MongoParseError: Incomplete key value pair for option
    at parseQueryString (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:387:13)
    at parseConnectionString (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:447:21)
    at QueryReqWrap.dns.resolveTxt [as callback] (/Users/jonathankuhl/Documents/Programming/JavaScript/Vue/100daysv3/node_modules/mongodb-core/lib/uri_parser.js:127:7)
    at QueryReqWrap.onresolve [as oncomplete] (dns.js:245:10)
  name: 'MongoParseError',
  [Symbol(mongoErrorContextSymbol)]: {} }

以下是连接代码,其中大部分直接从《 Mongoose入门指南》中提取和修改,因为我以前从未使用过Mongoose:

const mongoose = require('mongoose');
const user = process.env.MONGO_USER;
const pw = process.env.MONGO_PW;
const uri = `mongodb+srv://${user}:${pw}@cluster0-vji0s.mongodb.net/100days?test?retryWrites=true`;

// wrap the connection in a closure so I can export it with the URI
function Connect(uri) {
    return function() {
        mongoose.connect(uri, {
            auth: {
                user: user,
                password: pw
            }
        });

        const db = mongoose.connection;
        db.on('error', console.error.bind(console, 'connection error'));
        db.once('open', ()=> {
            console.log('\nSuccessfully connected to Mongo!\n');
        });
    }
}

const MongooseConnect = Connect(uri);

module.exports = MongooseConnect;

在我的服务器(Express JS)中,一旦服务器运行,我就连接了:

const MongooseConnect = require('./database.js');

//... other express stuff

app.listen(port, ()=> {
    MongooseConnect();
    console.log(`listening on port ${port}\n`)
});

该代码有效,我连接并访问了我的数据库,然后执行CRUD。我并非总是会遇到此错误(嗯,不是密钥对错误,我一直都有过时的警告,只是稍后将罐子踢了下去),但是我不知道是什么触发了更改它的错误。我所做的最新更改是将架构移动到它们自己的文件夹,我不知道为什么会导致这种情况。我不知道是什么原因导致这些错误消息出现,或者如何使它们消失。我在上面所做的链接中尝试的所有操作均无效或使情况更糟。

任何帮助将不胜感激

此外,我的用户名和密码不使用任何特殊字符,仅使用数字和字母。

3 个答案:

答案 0 :(得分:1)

要解决newUrlParser错误,您只需要将Mongoose connect调用更改为:

mongoose.connect(uri, {
    auth: {
        user: user,
        password: pw
    },
    useNewUrlParser: true 
});

为了确定是否可以解决其他问题。

编辑:我想我知道是什么引起了另一个问题

您的连接字符串是:

mongodb+srv://${user}:${pw}@cluster0-vji0s.mongodb.net/100days?test?retryWrites=true

有问题的部分是:/100days?test?retryWrites=true

这不是正确的查询字符串。我不确定您是否要分配test参数,但现在尝试/100days?retryWrites=true

如果查询字符串中确实需要多个参数,则语法为/100days?retryWrites=true&secondParam=value&thirdParam=thing ...

错误是说test参数没有价值,因为没有=value部分。它可能会丢弃整个查询字符串。因此,您的代码应该正常运行,而无需设置retryWrites=true部分。

希望有帮助!

答案 1 :(得分:1)

这里有两个不同的错误。

要修复current URL string parser is deprecated,请参阅布兰登的答案(使用userNewUrlParser)。

要修复MongoParseError: Incomplete key value pair for option,您拥有的连接字符串没有有效的URI:

const uri = `mongodb+srv://${user}:${pw}@cluster0-vji0s.mongodb.net/100days?test?retryWrites=true`;

请注意/100days?test?retryWrites...部分。错误是说test不是有效的键值对。删除100days或删除test,例如:

mongodb+srv://${user}:${pw}@cluster0-vji0s.mongodb.net/test?retryWrites=true

或:

mongodb+srv://${user}:${pw}@cluster0-vji0s.mongodb.net/100days?retryWrites=true

有关URI格式的信息,请参见Uniform Resource Identifier

答案 2 :(得分:0)

只需更改您使用的密码,即可使用