0
0

More than 1 year has passed since last update.

'useFindAndModify' が有効なオプションでない時のエラー (mongoose を使ってmongoDBに接続する)

Posted at

エラー

Node.jsにおいてmongooseでmongoDBに接続するコードで下記のように書くと

エラーコード

mongoose.set('useFindAndModify', false);

await mongoose.connect('connectionURL', {
  useNewUrlParser: true, 
  useUnifiedTopology: true 
});
Error: `useFindAndModify` is an invalid option

上記のようなエラーが起こる。

原因

原因としては、不要なコードがあることが考えられます。

mongoose.set('useFindAndModify', false);

解決法

なので、上記のコードを次のように書き換えます。


mongoose.set('useFindAndModify', false); //いらない

mongoose
  .connect('connectionURL', {
    useNewUrlParser: true,   //いらない
    useUnifiedTopology: true,  //いらない
  })
  .then(() => {
//接続に成功
    console.log('Success!! MongoDB connection ');
  })
  .catch((err) => {
//接続に失敗
    console.log('Failed MOngoDB connection');
    console.log(err);
  });

参考

0
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
0
0