質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

Q&A

解決済

2回答

876閲覧

MySQLに接続できない

Kino104

総合スコア10

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

0グッド

0クリップ

投稿2021/09/01 05:16

ディレクトリ構造

![イメージ説明

ソースコード

JavaScript

1// index.js 2 3const express = require("express"); 4const app = express(); 5const cors = require("cors"); 6const mysql = require("mysql"); 7 8const db = mysql.createConnection({ 9 host: "localhost", 10 user: "root", 11 password: "zuvm8152", 12 database: "Kiroku" 13}); 14 15app.use(express.json()); 16app.use(cors()); 17 18// Routers 19const usersRouter = require("./routes/Users"); 20app.use("/auth", usersRouter); 21 22app.listen(3001, () => { 23 console.log("Server running on port 3001"); 24}); 25 26module.exports = db;

JavaScript

1// Users.js 2 3const express = require("express"); 4const router = express.Router(); 5const db = require('../index'); 6const bcrypt = require("bcrypt"); 7 8router.post("/", async (req, res) => { 9 const {username, password} = req.body; 10 const sqlInsert = "INSERT INTO Users (username, password) VALUES (?, ?)"; 11 bcrypt.hash(password, 10).then((hash) => { 12 db.query(sqlInsert, [username, hash], (err, result) => { 13 console.log(result); 14 }); 15 }); 16}); 17 18module.exports = router;

エラーコード

(node:83857) Warning: Accessing non-existent property 'query' of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
(node:83857) UnhandledPromiseRejectionWarning: TypeError: db.query is not a function
at /Users/k20081kk/App-Project/Kiroku/server/routes/Users.js:10:8
(node:83857) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:83857) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

概要

ユーザ登録機能を作ろうとしています。
User.jsで入力されたユーザーネームとパスワードをデータベースに保存したいのですが、db.query is not a functionとなり、MySQLとの接続が上手くいきません。
なぜ上手くいかないのかわからないので、教えてください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

mysql接続用のファイルを新しく作り、そのファイルをUsers.jsでインポータントしたら上手くいきました。

投稿2021/09/02 04:10

Kino104

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

TypeError: db.query is not a function at /Users/k20081kk/App-Project/Kiroku/server/routes/Users.js:10:8

とありますので、とりあえずUsers.jsの10行目8桁目あたりを調べてください。

また、mysql2を使ってみましょう。
require("mysql2");

VisualStudioCodeを使っているなら、デバッグモードで1行ずつ実行して、変数の中身などを
確認するのもよいでしょう。

投稿2021/09/01 11:27

編集2021/09/01 11:54
technocore

総合スコア7211

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問