node.jsのmongoose.connect()時のエラー
エラー内容
(node:22215) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Google翻訳で翻訳
DeprecationWarning:現在のURL文字列パーサは廃止され、将来のバージョンで削除されます。 新しいパーサを使用するには、オプション{useNewUrlParser:true}をMongoClient.connectに渡します。
あとこちらの記事「MongoClient.connect()時にuseNewUrlParserオプションを与える」も参考にし、第二引数に{ useNewUrlParser: true }
を追加するとエラーが消えた。
node.js
const express = require('express');
const mongoose = require('mongoose');
mongoose.connect('mongodb://(省略).mlab.com:(省略)', { useNewUrlParser: true });
const app = express();
app.get('/rentals', function(req, res) {
res.json({'success': true});
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, function() {
console.log('I am runnnig');
});
とても助かりました。