title: Prevent MySQL/Node.js webapp from automatically repeating queries to database
body: <p>I'm working on a MERN webapp (using MySQL instead of MongoDB) and we are having an issue where at some point after querying to add an entry, the backend is somehow re-querying the last query. In the meantime, between the original entry and the duplicate one, the backend runs continuously to keep the frontend displaying up-to-date data.</p>
  
  <p>I've read on old threads that MySQL for Node may have some issues with memory leaking and thus running sleep queries. I don't believe this is a frontend issue, and the backend isn't running the queries as if they were called from the function that handles the frontend button click (to submit).</p>
  
  <p>I'm a noob to full-stack JS, so I don't really know where to look for an answer to this issue. I'm not sure if we should band-aid the solution and just make sure a query isn't sent without the submit button being clicked within a 1 second time span.</p>
  
  <p>EDIT: Here's code for the query to add the entry (data for a music album).</p>
  
  <pre><code>db.query(insertAlbumQuery, insertAlbumValues, function(err, result) {
        if (err) throw err;
        console.log("1 Album added.");
      });
  </code></pre>
  
  <p>We don't close the connection to the db after querying, one connection runs for the duration of the app's execution.</p>
  
  <p>Here's the code for the db connection:</p>
  
  <pre><code>let mysql = require("mysql");
  var db;
  
  function connectDB() {
    if (!db) {
      db = connection = mysql.createConnection({
        host: "******",
        user: "******",
        password: "******",
        database: "******"
      });
  
      connection.connect(function(err) {
        if (err) {
          return console.error("error: " + err.message);
        } else {
          console.log("Connected to the MySQL server.");
        }
      });
    }
    return db;
  }
  
  module.exports = connectDB();
  </code></pre>
  
  <p>From the main, backend app.js file we have the router post</p>
  
  <pre><code>router.post("/addAlbum", (req, res) =&gt; {
      const {
          Album_title,
          Artist,
          Release_date,
          Category,
          Description,
          Rotation
  
      } = req.body;
  
      album.add(Album_title, Artist, Release_date, Category, Description, Rotation, null);
  });
  </code></pre>
  
  <p>From the front end we post with</p>
  
  <pre><code> addAlbum = currentState =&gt; {
      this.setState({ Rotation: +this.state.Rotation });
      axios.post("http://localhost:3001/api/addAlbum", this.state);
    };
  </code></pre>
  
  <p>Not sure if this is enough background on the app, but I can add more snippets if needed.</p>