MongooseでlocalhostのmongoDBに接続できない原因
概要
localhostだと接続できないが127.0.0.1だと接続できるとのこと。
OK
mongodb://127.0.0.1:27017/xxx
import mongoose from 'mongoose'
const connectDB = async () => {
try {
await mongoose.connect('mongodb://127.0.0.1:27017/xxx');
console.log("connected to MongoDB.");
} catch (err) {
console.log("unconnected to MongoDB.");
throw new Error();
}
};
export default connectDB;
NG
mongodb://localhost:27017/xxx
参考サイト
https://stackoverflow.com/questions/70309589/mongoose-cant-connect-localhost