Node.js Mongoose连接到身份验证数据库语法,包括auth和useNewUrlParser选项

Node.js Mongoose连接到身份验证数据库语法,包括auth和useNewUrlParser选项,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在尝试本地连接到身份验证数据库以访问我的应用数据库。如果我包括useNewUrlParser:true选项,它会破坏整个过程,导致身份验证失败。删除useNewUrlParser选项将连接但不勾选此选项: config = { database: "mongodb://localhost:27017/authapp", auth: { user : "admin", password : "123456", authdb : "

我正在尝试本地连接到身份验证数据库以访问我的应用数据库。如果我包括
useNewUrlParser:true
选项,它会破坏整个过程,导致身份验证失败。删除
useNewUrlParser
选项将连接但不勾选此选项:

config = {
    database: "mongodb://localhost:27017/authapp",
    auth: {
        user : "admin",
        password : "123456",
        authdb : "admin"
    }

...

mongoose
  .connect(config.database, { 
        auth: config.auth,
        useNewUrlParser: true 
    })
  .then(() => {...}
我得到:

[nodemon]正在启动
节点索引.js
(节点:17814)弃用警告: 当前服务器发现和监视引擎已弃用,将 将在将来的版本中删除。要使用新服务器,请执行以下操作: 监视引擎,将选项{useUnifiedTopology:true}传递给 MongoClient构造函数。应用程序正在4000上运行{数据库\错误: MongoNetworkError:无法连接到上的服务器[localhost:27017] 首次连接[MongoError:身份验证失败。 at Function.\u getError(/Users/bpav/vue/new\u app/authapp/server/node\u modules/mongodb/lib/core/auth/scram.js:141:14) at/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:191:31 在回调时(/Users/bpav/vue/new\u app/authapp/server/node\u modules/mongodb/lib/core/connection/connect.js:320:5) 位于Connection.messageHandler(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/Connection/connect.js:349:5) 在Connection.emit(events.js:321:20) 在processMessage(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:384:10) 在套接字上。(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:553:15) 在Socket.emit(events.js:321:20) 在addChunk(_stream_readable.js:305:12) 在readableAddChunk(_stream_readable.js:280:11) 在Socket.Readable.push(_stream_Readable.js:214:10) 在TCP.onStreamRead(internal/stream_base_commons.js:186:23){ 名称:“MongoError”, [符号(mongoErrorContextSymbol)]:{}] 在池中。(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/topologies/server.js:433:11) 在Pool.emit(events.js:321:20) at/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/pool.js:577:14 at/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/pool.js:1007:11 在回调时(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:93:5) at/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connect.js:367:21 在/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11 at/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/auth/scram.js:193:16 在回调时(/Users/bpav/vue/new\u app/authapp/server/node\u modules/mongodb/lib/core/connection/connect.js:320:5) 位于Connection.messageHandler(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/Connection/connect.js:349:5) 在Connection.emit(events.js:321:20) 在processMessage(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:384:10) 在套接字上。(/Users/bpav/vue/new_app/authapp/server/node_modules/mongodb/lib/core/connection/connection.js:553:15) 在Socket.emit(events.js:321:20) 在addChunk(_stream_readable.js:305:12) 在readableAddChunk(_stream_readable.js:280:11){ 名称:“MongoNetworkError”, [符号(mongoErrorContextSymbol)]:{}}

我是否缺少一种模式,或者什么方法适用于这种情况?我还尝试了数据库连接字符串中的user+pw,但它也没有按预期连接-可能缺少身份验证数据库名称


希望有人能为我指出这个简单问题的正确方向。

使用authSource而不是authdb

 auth:{
     user: "admin",
     password: "123456",
     authSource: "admin",                                 
    },

你能编辑你的答案以包含解释吗?为什么是authSource?