-1

EDITED:

I am working on a page that takes the information a user enters into a form and adds it to a database. I created the database with mysql. When trying to connect to my database using javascript. My code breaks on var mysql = require('mysql');:

I realized that this may be due to either the code being ran in a function or having the DOM related code in the function...

I tried running a simple version of the code without those elements and it works to connect to and update the database but I need to be able to pull the values from the form so I am still struggling with how to do this.

function saveUserForm() {
    var firstName = document.getElementById("firstName");
    var lastName = document.getElementById("lastName");
    var psw = document.getElementById("psw");
    var userName = document.getElementById("userName");
    var email = document.getElementById("inputText");
    alert('test');
    var mysql = require('mysql');
    alert("mysql test");
    var con = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "abc123",
      database: "PBSC_Parking_DB"
    });

    con.connect(function(err) {
     if (err){
         throw err;
         alert("error");
     }

    var sql = "INSERT INTO accounts (UserName, FirstName, LastName, Email, UserPassword) VALUES ('"+userName+ "', '"+firstName+"','"+lastName+"','"+email+"','"+psw+"')";

      con.query(sql, function (err, result) {
        if (err) {
            throw err;
            alert("test error");
        }
        alert("account added");
        console.log(result.affectedRows + " record(s) updated");
      });
    }); 
} 

3 Answers3

0

you can try writing npm i mysql in the built in terminal of your ide instead of cmd and check that the mysql folder is added to your project dependencies or you can run npm i --g mysql to install that globally

mohammadtz55
  • 131
  • 1
  • 11
  • how do I check that it's added to my project dependencies? I did already try npm install mysql -g – Alexandra Amara Apr 16 '20 at 20:23
  • in the root folder of your project there is a folder called node_modules. check that folder the mysql folder should be there if its successfully installed – mohammadtz55 Apr 20 '20 at 08:10
0

require('mysql') is async, so it needs to be treated as a promise. For safety, I would load this outside of the function scope. The only time you would need to require something inside the scope of a function is for lazy loading local assets.

Raz Chiriac
  • 406
  • 2
  • 7
0

You try to include a library with node from cmd so that's normal to give you undefined. cause node declares values like this. so you seem to install MySQL with npm globally. so it appears with you.

enter image description here

that's not related to your code at all. you need to produce bug in your code via log error or debug code!

Omar Ghazi
  • 161
  • 3
  • 7
  • it doesn't give an error... it just doesn't work. The alert doesn't alert on line 9 doesn't display is how i know it's breaking there. – Alexandra Amara Apr 16 '20 at 20:46
  • Cause you shouldn't use **document** in the backend. it's related to DOM [front-end] . so remove the first 8th lines from code then run the file – Omar Ghazi Apr 16 '20 at 20:52
  • I don't understand without document how I can pull the values from the form... but I tried taking them out for now and it still doesn't work. – Alexandra Amara Apr 16 '20 at 20:57