[英]node.js mongoError not authorized on admin to execute command
I'm studying Node.js and i can'y find any solution.. my consol send me many messages an one is more particulary strange : GET/signup - - ms - - has someone any idea about that ?我正在学习 Node.js,但找不到任何解决方案。我的控制台向我发送了许多消息,其中一个更特别奇怪:GET/signup - - ms - - 有人对此有任何想法吗? Thanks in advance !
提前致谢 !
///////////////////////inclusion des librairies // 3 librairies pour gérer les messages flash var session = require('express-session'); var cookieParser = require('cookie-parser'); var flash = require('express-flash'); //passerelle pour se connecter à node(node->bdd) var passport =require('passport'); // stockage des sessions(id) et cookies côté serveur uniquement var mongoStore =require('connect-mongo')(session); // le session de express-session //inclure al librairie express var express = require('express'); // Inclusion de la librairie morgan (faire le lien avec la base de données) var morgan = require('morgan'); // Inclusion de mongoose var mongoose = require('mongoose'); //Inclusion moteur templates ejs var ejs = require('ejs'); var engine =require('ejs-mate'); // Inclusion de body parser pour les données des formulaires var bodyParser = require('body-parser'); /////////////////////fin des librairies /////////////// // stocker l'objet express dans une variable plus courte var app = express(); //inclure le fichier secret.js var secret = require('./config/secret'); ////////////connexion à la bd avec mongoose/// mongoose.connect(secret.database, // voir pour création de db en ligne !! {useNewUrlParser:true}, function(err){ if(err){console.log(err) }else{ console.log('connexion OK'); } }); /////////////////// gestion des Passerelles (middleware)///////////////////////// app.use(express.static(__dirname + '/public')); // pour le style app.use(morgan('dev')); app.engine('ejs',engine); app.set('view engine','ejs'); // les deux lignes ci-dessous pour récupérer les données des formulaires app.use(bodyParser.json()); app.use(bodyParser.urlencoded({extended:true})); // affichage messages flash et gestion des cookies app.use(cookieParser()); app.use(session({ resave :true, saveUninitialized:true, secret :secret.secretKey, store : new mongoStore({ url:secret.database, autoReconnect:true }) })); app.use(flash()); //authentification app.use(passport.initialize()); app.use(passport.session()); ////////////////définition du chemin des pages principales//////////////////////// var mainRoutes =require('./routes/main'); app.use(mainRoutes); var userRoutes =require('./routes/user'); app.use(userRoutes); //app.post() //app.put() //app.delete()
AND THE CONSOLE SAYS :控制台说:
Le serveur est lancé sur le port3000 (node:7828) DeprecationWarning: collection.ensureIndex is deprecated.
Le serveur est lancé sur le port3000 (node:7828) DeprecationWarning: collection.ensureIndex 被弃用。 Use createIndexes instead.
请改用 createIndexes。 connexion OK (node:7828) UnhandledPromiseRejectionWarning: MongoError: not authorized on admin to execute command { insert: "system.indexes", documents: [[{ns admin.sessions} {key [{expires 1}]} {name expires_1} {expireAfterSeconds 0} {unique false}]], ordered: true } at Function.MongoError.create (C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\error.js:31:11) at C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\pool.js:497:72 at authenticateStragglers (C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\pool.js:443:16) at Connection.messageHandler (C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\pool.js:477:5) at TLSSocket.
连接正常(节点:7828)未处理的PromiseRejectionWarning:MongoError:管理员未授权执行命令{插入:“system.indexes”,文档:[[{ns admin.sessions} {key [{expires 1}]} {name expires_1} {expireAfterSeconds 0} {unique false}]],在 Function.MongoError.create (C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\error .js:31:11) 在 C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\pool.js:497:72 at authenticateStragglers (C:\\ Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\pool.js:443:16) 在 Connection.messageHandler (C:\\Users\\Utilisateur\\Desktop\\NODE\\ ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\pool.js:477:5) 在 TLSSocket。 (C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\connection.js:333:22) at TLSSocket.emit (events.js:182:13) at addChunk (_stream_readable.js:283:12) at readableAddChunk (_stream_readable.js:264:11) at TLSSocket.Readable.push (_stream_readable.js:219:10) at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) (node:7828) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
(C:\\Users\\Utilisateur\\Desktop\\NODE\\ECommerce\\node_modules\\connect-mongo\\node_modules\\mongodb-core\\lib\\connection\\connection.js:333:22) 在 TLSSocket.emit (events.js:182:13) ) at addChunk (_stream_readable.js:283:12) at readableAddChunk (_stream_readable.js:264:11) at TLSSocket.Readable.push (_stream_readable.js:219:10) at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons .js:94:17) (node:7828) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 1) (node:7828) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
(rejection id: 1) (node:7828) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。 GET /login - - ms - - GET /login - - ms - - GET /login - - ms - - GET /signup - - ms - -
GET /login - - ms - - GET /login - - ms - - GET /login - - ms - - GET /signup - - ms - -
The npm module connect-mongo
can't handle mongodb+srv://
connection strings. npm 模块
connect-mongo
无法处理mongodb+srv://
连接字符串。 You'll have to use the older connection string types that start with mongodb://
.您必须使用以
mongodb://
开头的旧连接字符串类型。
If you are using MongoDB Atlas I recommend to go to connect on the cluster view, then connect your application and then select Node.js version 2.2.12, not 3.0 .如果您正在使用MongoDB的阿特拉斯我建议去群集视图上的连接,然后连接您的应用程序,然后选择Node.js的版本2.2.12,而不是3.0。
You probably also have to change the /test
or /admin
in your connection string to /TheNameOfYourDatabase
with your database name there.您可能还必须将连接字符串中的
/test
或/admin
更改为/TheNameOfYourDatabase
并在那里使用您的数据库名称。 (See radihuq's answer below) (见下面radihuq的回答)
@ConstJS answer worked for me. @ConstJS 答案对我有用。 Expanding on it a little bit -
稍微扩展一下 -
If you're using Postman and your error looks like this:如果您使用的是 Postman 并且您的错误如下所示:
"errmsg": "not authorized on admin to execute command { insert: [..] }, $db: \\"admin\\" }"
"errmsg": "未授权管理员执行命令 { insert: [..] }, $db: \\"admin\\" }"
Note this specific part:注意这个特定部分:
$db: \\"admin\\"
$db: \\"管理员\\"
You'll want to change that to your DB name so go to your URI and find您需要将其更改为您的数据库名称,因此请转到您的 URI 并找到
mongodb.net:27017/admin
and change admin to your database name并将 admin 更改为您的数据库名称
It appears you are trying to access MongoDB's admin database and insert something which is not allowed.您似乎正在尝试访问 MongoDB 的管理数据库并插入不允许的内容。 I am assuming you are using a connection string like:
我假设您正在使用如下连接字符串:
mongodb://mongodb0.example.com:27017/admin
Make sure you update the /admin
piece to the database you want to connect to.确保将
/admin
部分更新为要连接的数据库。
This error is specific to the fact that you are passing a url mongo-connect
doesn't know how to handle.此错误特定于您传递的 url
mongo-connect
不知道如何处理的事实。 Instead of the URL, you can pass the mongoose.Connection
object and use the mongooseConnection
property in the MongoStore
options object.取而代之的是网址,你可以通过
mongoose.Connection
对象,并使用mongooseConnection
在物业MongoStore
选择对象。
new mongoStore({
mongooseConnection: mongoose.connection,
autoReconnect: true
})
In order for this to work, you'll need to establish a connection first and then pass the mongoose.connection
object to your mongoStore
class.为了使这一工作,你需要先建立连接,然后通过
mongoose.connection
对象添加到您mongoStore
类。
mongoose.connect(secret.database, {useNewUrlParser:true}, function(err) {
if(err) {
console.log(err)
} else {
app.use(session({
resave :true,
saveUninitialized:true,
secret :secret.secretKey,
store : new mongoStore({
url:secret.database,
autoReconnect:true })
}));
console.log('connexion OK');
}
});
There are more elegant ways to handle it but that should get you started.有更优雅的方法来处理它,但这应该会让你开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.