前提
MongoDBを初めて触るものです。
YouTubeを見ながら、Node.jsとMongoDBを接続させて簡単なAPIを作成しています。
https://youtu.be/JBCykXLfvv0
Node.jsは接続できているものの、MongoDBが接続できない状態です。
ドキュメントを読んで実行してみても、以下のエラーが出力されてしまう状態です。
コード
実現したいこと
・MongoDBとNode.jsの接続
発生している問題・エラーメッセージ
[nodemon] restarting due to changes... [nodemon] starting
node server.js(node:50620) [MONGOOSE] DeprecationWarning: Mongoose: the
strictQueryoption will be switched back to
falseby default in Mongoose 7. Use
mongoose.set('strictQuery', false);if you want to prepare for this change. Or use
mongoose.set('strictQuery', true);to suppress this warning. (Use
node --trace-deprecation ...` to show where the warning was created)
サーバーが起動しました
node:internal/errors:484
ErrorCaptureStackTrace(err);
^
Error: querySrv EREFUSED _mongodb._tcp.cluster0.r9kvdi2.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:251:17) {
errno: undefined,
code: 'EREFUSED',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.r9kvdi2.mongodb.net'
}
Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting... `
該当のソースコード
J Sファイル1
const express = require("express"); const app = express(); const mongoose = require("mongoose"); //データベース接続 mongoose.connect( "mongodb+srv://ユーザー名:Password@cluster0.r9kvdi2.mongodb.net/food?retryWrites=true&w=majority" ).then(() => console.log("success")); app.listen(3000, ()=>{ console.log("サーバーが起動しました"); });
JSファイル2
const mongoose = require("mongoose"); const foodSchema = new mongoose.Schema({ name: { type: String, required: true, trim: true, //空白削除 lowercase: true, }, calories: { type: Number, default: 0, validate(value){ if(value < 0) throw new Error("マイナスのカロリーは存在しません"); }, }, }) const Food = mongoose.model("Food",foodSchema); module.exports = Food;
試したこと
・公式ドキュメント通りに試した
・他の解説動画などを確認した
初心者質問で申し訳ございませんが、どなたかご教授いただけますと幸いです。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。