0

I'm new to Node.js I'm testing some code on Wix to check my database if a account name already exists prior to allowing a new one to be created (I'm purposely not using the WHERE tag at the moment for learning purposes).

Currently the method check account name returns before the connection finishes, not allowing the check to take place properly.

Any help appreciated.

    export function tryToCreateAccount(login, password) 
{
    var mysql = require('mysql');

    var connection = mysql.createConnection({
        host: 'host',
        user: 'user',
        password: 'pass',
        database: 'db'
    });

    if(checkAccountName(login, connection))
    {
        console.log("Name didn't exist.");
    }
    else
    {
        console.log("Name Existed.");
    }
}

function checkAccountName(account_name, connection)
{
    var accountNameAvailable = true;

    connection.connect(function (err)
    {
        if(err) throw err;
        connection.query("SELECT login FROM accounts", function (err, result)
        {
            if (err) throw err;

            for(var i = 0; i < result.length ; i++)
            {
                if(result[i].login == account_name)
                {
                    console.log("Should of been false");
                    connection.end;
                    accountNameAvailable = false;
                }
            }

        });
        connection.end;
    });

    return accountNameAvailable;
}

2 Answers 2

1

I figured out why it wasn't doing anything, the next was getting called too late since the connection ended and next was within the connection code block.

const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'host',
    user: 'user',
    password: 'pass',
    database: 'db'
});

export function tryToCreateAccount(login, password) 
{
    checkAccountName(login, connection, function(err, accountNameAvailable)
    {
      if(err || !accountNameAvailable){
        console.log("Name didn't exist.");
      }
      else
      {
        console.log("Name Existed.");
      }
   })
}

function checkAccountName(login, connection, next)
{
    var accountNameAvailable = false;

    connection.connect(function (err)
    {
        if(err) next(err);
        connection.query("SELECT login FROM accounts", function (err, result){
            if (err) next(err);
            for(var i = 0; i < result.length ; i++)
            {
                if(result[i].login == login)
                {
                    accountNameAvailable = true;
                }
            }
            next(null, accountNameAvailable);
            connection.end();
        });
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Glad to see you figured it out. I wasn't actually able to test my code since I didn't want to take the time to set up a database :)
I made a new question about callbacks as for some reason adding a callback to the tryToCreateAccount breaks it, maybe you have some insight? link
0

Welcome to Node.js (and the world of Async functions (and Promises (and Callbacks)))

I've written this in the "callback" style, but I highly recommend looking into async/await for something like this, as well as understanding how "promises" fit into the picture.

// to test, call tryToCreateAccount('login','pass',function(err,data){console.log(err,data)});

const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'host',
    user: 'user',
    password: 'pass',
    database: 'db'
});
export function tryToCreateAccount(login, password, next) 
{
    checkAccountName(login, connection, function(err, accountNameAvailable){
      if(err || !accountNameAvailable){
        console.log("Name didn't exist.");
        next(err || 'Name didn't exist.')
      }
      else
      {
        console.log("Name Existed.");
        next(null, true)
      }
   })
}

function checkAccountName(account_name, connection, next)
{
    var accountNameAvailable = false;

    connection.connect(function (err)
    {
        if(err) next(err);
        connection.query("SELECT login FROM accounts", function (err, result){
            if (err) next(err);
            for(var i = 0; i < result.length ; i++)
            {
                if(result[i].login == account_name)
                {
                    console.log("Should of been false");
                    connection.end;
                    accountNameAvailable = true;
                }
            }
            connection.end();
            next(null, accountNameAvailable);
        });

    });
}

1 Comment

Appreciate the response, think I understand callbacks from this now at least hah, problem is now nothing happens. I removed the call back from tryToCreateAccount for simplicity while testing, as it isn't currently required. When I run, I get nothing in the console. I added a few console.log statements to find where it kind of stops. It enters the try to create, then it enters into the checkAccountName, and then stops. If I put a statement inside of the connection.connect code block it never shows up. No errors are thrown.

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.