Home > database >  Node.JS and MYSQL How do I store a column value into a variable
Node.JS and MYSQL How do I store a column value into a variable

Time:11-28

So I have been working on a registration/login system for a theoretical bank and I'm trying to create accounts for the user who is signing up and adding it under their user ID however I'm not sure how to take the query from the database and put it into a variable so I can do this.

This is more or less how I'm trying to do it. However I'm not sure how to put the information that I'm grabbing into the userID variable

db.query('Select userID FROM users WHERE userEmail = ?', email, (error, results) => {
        if (error) {
            console.log(error);
        }

        var userID = <userID from the database>

        if(userID !== undefined)
        {
            db.query('INSERT INTO accounts SET ?', {users_userID: userID, accountType: 'Checking', accountBalance: 100}, (error, results) =>{
                console.log('creating checking');
                if (error) {
                    console.log(error);
                }
            });

            db.query('INSERT INTO accounts SET ?', {users_userID: userID, accountType: 'Savings', accountBalance: 100}, (error, results) =>{
                console.log('creating savings');
                if (error) {
                    console.log(error);
                }
            });
        }
    });

CodePudding user response:

The results variable should contain the data retrieved from the db query.

Example (if there is a field called 'userID'):

db.query(... (error, results) => {
    var userID = results[0].userID;
}
  • Related