LoginSignup
1
0

More than 3 years have passed since last update.

【MongoDB】 "Unhandled rejection MongoError: (Unauthorized) not authorized..."の対処法

Posted at

Mongooseを使っているときによくあるエラーかと思ったので共有します。

Unhandled rejection MongoError: (Unauthorized) not authorized on admin to execute command { listIndexes: "sessions", cursor: { } }

こちらのエラーに関してです。

僕の場合、この原因はExpressのセッション処理が原因でした。
以下の部分です。

server.js
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: 'aaabbbccc',
  store: new MongoStore({
    url: MONGO_URI,
    autoReconnect: true
  })
}));

こちらを以下のように書き換えます。

server.js
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: 'aaabbbccc',
  store: new MongoStore({
    mongooseConnection: mongoose.connection,
    autoReconnect: true
  })
}));

これで解決しました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0