使用mongoose连接Mlab

5

嗨,我正在尝试建立一个简单的连接。

mongoose.connect('mongodb://JFalcon:john2522@ds119476.mlab.com:19476/hidonshabat', {useMongoClient: true}, function(err){
    if(err) {
        console.log('Some problem with the connection ' +err);
    } else {
        console.log('The Mongoose connection is ready');
    }
})

你可以看到这是URL链接!请帮我。


1
什么错误? - Subburaj
错误的凭据? - Alex Blex
那么,问题是什么?另外,当您发布此类问题时,请记得从连接字符串中删除密码,例如在这种情况下的 john2522 :D。 - Pramesh Bajracharya
这只是测试。但谢谢。 问题是: 无法加载资源:服务器响应状态为502(错误网关)。 - John Yaghobieh
3个回答

5
请按以下方式进行更正:
mongoose.connect('mongodb://<dbuser>:<dbpassword>@ds119476.mlab.com:19476/hidonshabat', 
    {useNewUrlParser: true },function(err)=>{
    {
        if(err) {
            console.log('Some problem with the connection ' +err);
        } else {
            console.log('The Mongoose connection is ready');
        }
    })

1
mongoose.connect('mongodb://localhost:27017/myapp', **{useNewUrlParser: true}**);

来自官方文档:

useNewUrlParser - 底层的 MongoDB 驱动程序已经弃用了他们当前的连接字符串解析器。因为这是一个重大的变化,他们添加了 useNewUrlParser 标志以允许用户在发现新解析器中存在错误时返回旧解析器。除非无法连接,否则应将useNewUrlParser: true设置为true。请注意,如果您指定了useNewUrlParser: true,则必须在您的连接字符串中指定端口,例如mongodb://localhost:27017/dbname。新的url解析器不支持没有端口的连接字符串,例如mongodb://localhost/dbname。


0
var Mongoose=require("mongoose");
var dbURI='mongodb://JFalcon:john2522@ds119476.mlab.com:19476/hidonshabat';
Mangoose.connect(dbURI,function(err){    
    if(err){
    console.log('Some problem with the connection ' +err)   
    } 
    else {
    console.log('The Mongoose connection is ready')  
    }

})
module.exports={Mongoose};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接