Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

mongodb开启auth,mongoose需要怎么连接?

$
0
0

现在目前的情况是控制台和用工具连接都没有问题,但是mongoose查询数据时就会报未授权的错误。

  • 代码如下:
const connect =  ()=> {mongoose.connect('mongodb://username:password@127.0.0.1:27017/database');};
connect();
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.on('disconnected', connect);
  • 然后试了这样子 还是不行
var options = {
    db: { native_parser: true },
    server: { poolSize: 5 },
    replset: { rs_name: 'userAdmin' },
    user: xxx
    pass: xxx
}
const connect =  ()=> {mongoose.connect('mongodb://xxx:xxx@127.0.0.1:27017/xxx',options)};
connect();
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.on('disconnected', connect);
var options = {
    db: { native_parser: true },
    server: { poolSize: 5 },
    replset: { rs_name: 'userAdmin' },
    user: xxx
    pass: xxx
}
const connect =  ()=> {mongoose.connect('mongodb://127.0.0.1:27017/xxx',options)};
connect();
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.on('disconnected', connect);

都不行,这个要怎么破?


Viewing all articles
Browse latest Browse all 14821

Trending Articles