Good day, I’m currently trying to code a account creation form, for obvious reason I need all the MySQL database connection information on the backend, and the whole form is handled on the frontend page. On the front end I call an exported function from the backend web module, however if I try to have the exported function have a call back the code fails. Is there anyway to get a response back to the front end?
I need to handle what the page does if the account creation is successful or not.
Cheers,
Matt
Front End Code:
import { tryToCreateAccount } from 'backend/account.jsw';
export function create_account_clicked(event)
{
tryToCreateAccount($w('#input1').value, null, null, function(err, data)
{
console.log(data);
};
}
Back-End Code:
const mysql = require('mysql');
const pool = mysql.createPool(
{
connectionLimit : 10,
host : 'hostname',
user : 'user',
password : 'pass',
database : 'db'
});
export function tryToCreateAccount(login, password, passwordConfirm, callback)
{
checkAccountNameAvailable(login, pool, function(err, accountNameAvailable)
{
callback(err, accountNameAvailable);
});
}
function checkAccountNameAvailable(login, pool, callback)
{
var accountNameAvailable = true;
console.log("Checking account name", login);
pool.getConnection(function (err, connection)
{
console.log("Made a connection.");
if(err)
{
console.log(err);
}
connection.query("SELECT login FROM accounts WHERE login = ?", login, function (err, result)
{
console.log("Checking Query Method");
if (err)
{
console.log(err);
callback(err, null);
}
if(result.length > 0) accountNameAvailable = false;
console.log("returning ", accountNameAvailable);
callback(null, accountNameAvailable);
connection.destroy();
});
});
}