This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-17. For the latest, interactive version please visit https://mariadb.com/kb/.

db is not defined

Dear Sir or Madam!

I have a question. I have run the following code in Visual Studio in an environment of react.js. It is the db server. I have a client server that is running the form. If I run this code, the server drop this error:

ReferenceError: db is not defined at C:\node\SAMPLE-CRUD\server\index.js:31:2 at Layer.handle [as handle_request] (C:\node\SAMPLE-CRUD\server\node_modules\express\lib\router\layer.js:95:5) at next (C:\node\SAMPLE-CRUD\server\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (C:\node\SAMPLE-CRUD\server\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (C:\node\SAMPLE-CRUD\server\node_modules\express\lib\router\layer.js:95:5) at C:\node\SAMPLE-CRUD\server\node_modules\express\lib\router\index.js:284:15 at Function.process_params (C:\node\SAMPLE-CRUD\server\node_modules\express\lib\router\index.js:346:12) at next (C:\node\SAMPLE-CRUD\server\node_modules\express\lib\router\index.js:280:10) at C:\node\SAMPLE-CRUD\server\node_modules\body-parser\lib\read.js:137:5 at AsyncResource.runInAsyncScope (node:async_hooks:203:9)

Suppose I turn on this row. I have no mistake but the get request has not functioned. db=await pool.getConnection();

What is the problem? Thank you for your help

This is my server code: const express = require('express'); const app = express(); const PORT = 3001; const mariadb = require('mariadb'); const cors = require('cors');

app.use(cors()); app.use(express.json());

async function asyncFunction() {

const pool = mariadb.createPool({ host:"127.0.0.1", user:"molnarpe", password:"Ildiko1968", database:"employeesystem", connectionLimit: 10, port: 3307 });

app.post('/create', async (req, res) => { const name = req.body.name; const age = req.body.age; const country = req.body.country; const position = req.body.position; const wage = req.body.wage;

db=await pool.getConnection(); db.query("INSERT INTO employees (name, age, country, position, wage) VALUES (?, ?, ?, ?, ?)", [name, age, country, position, wage], (err,result) => { if (err) { console.log(err) } else { res.send("Values inserted") } } ) })

app.get("/employees", (req, res) =>{

pool.getConnection(); db.query("SELECT * FROM employees", (err, result) => { if (err) { console.log(err); } else { res.send(result); } }) })

app.listen(3001, () => { console.log("Server is running on port 3001"); });

module.exports = pool;

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.