0

I'm hosting a web app on localhost:3000 via NodeJS (with MySQL database via WAMP). Front end has a button to insert a record into a MySQL database table (called tblbacktrack). It runs the query as expected exactly 6 times, then stops working altogether, without an error or crash. 6 times, every time. If I change the query to a delete, same problem: 6 deletions is all I get until I refresh the page via the browser.

App2.js:

const express = require('express');
const mysql = require('mysql');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

// create connection
const db = mysql.createConnection({
    host: 'localhost', 
    user: 'root',
    password: '',
    database: 'journey_test'
})

 // connect to mysql
 db.connect(err => {
     if(err) {
         throw err
     }
     console.log('MySQL Connected');
 })

 const app = express()

 app.use(express.static('public'));
 app.set('view engine', 'ejs');
         

// This pulls index.ejs to the root folder location of the site.
app.get('/', function (req, res) {
    res.render('index2');
});


app.post('/deleteBtDb/', upload.none(), function (req, res, next) { 
    // let sql = `DELETE from tblbacktrack limit 1`
    let sql = `INSERT into tblbacktrack (btEntryNum, btDate) values ('E: 19810809', '2021-05-05');`
    let query = db.query(sql, (err, results) => {
            if(err) {
                    throw err
                }
        })
})

// Let the console know that the server has started on port 3000
 app.listen('3000', () => {
     console.log('Server Started on port 3000')
 })

index2.ejs:

<!DOCTYPE html>
  <html>
    <head>
    <link href="/css/style.css" rel="stylesheet" type="text/css" media="all" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 </head>
 <body>
    <div class="container">
        <div class="Header">
        </div>
        <div class="Footer"></div>
        <div class="Middle">
          <div id="Middle-Center" class="Middle-Center"></div>
          <div id="Middle-Left" class="Middle-Left"></div>
          <div id="Middle-Right" class="Middle-Right"><%- include ('../views/middle-right'); %></div>
        </div>
    </div>     
</body>

Middle-right.ejs:

    <div><button onclick="deleteBtDb()">Click me</button></div>
    <script>
    function deleteBtDb() {
        var xhttp = new XMLHttpRequest();
        xhttp.open("POST", "./deleteBtDb/", true);
        xhttp.send();
         }
    </script>

If I run the same queries in phpMyAdmin, they work, and I can definitely run them successfully more than 6 times there. If I set up a for loop in app2.js, such that a single click of the index2.ejs button should insert 10 new records, it successfully inserts 10 new records. Please help! I don't know how to further troubleshoot this.

2
  • what error it gives nodejs when it doesnt work ? (after 6 times) Commented Sep 8, 2021 at 23:14
  • No error in NodeJS. Just stops having results. Visual Studio Code terminal doesn't come up with anything new to tell me. [nodemon] starting node app2.js Server Started on port 3000 MySQL Connected Commented Sep 8, 2021 at 23:30

1 Answer 1

1

Solved it!

I wasn't using a "res" object in there. Added "res.status(201).json()", and it's all better now.

app.post('/deleteBtDb/', upload.none(), function (req, res, next) { 
    let sql = `DELETE from tblbacktrack order by btID ASC limit 1`
    let query = db.query(sql, (err, results) => {
        if(err) {
           throw err
           }
        })
res.status(201).json();
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.