asynchronous call to return a value? - javascript

I am bulding a table that either shows a play button or a stop button. (and some other stuff)
for(var i = 0; i < result.length; i++){
var current = result[i].split("$");
if (CheckRunning(current[0])){
t = t + "<tr><td><img alt='stop' id='img"+i+"' src='stop.png' onclick='ChangeButton(\"img"+i+"\");'/>";
} else {
t = t + "<tr><td><img alt='play' id='img"+i+"' src='play.png' onclick='ChangeButton(\"img"+i+"\");'/>";
}}
The problem here is the CheckRunning method. It opens a database which is an asynchron method. I cant simply do a return true/false. So whats the solution? anyway, here is the code for it:
var tabel;
var running = false;
function CheckRunning(tabel){
this.tabel = "tabel"+tabel+"";
var db = window.openDatabase(this.tabel, "1.0", this.tabel, 1000000);
db.transaction(checkrunningDB, checkerrorCB);
console.log(this.running);
return this.running;
}
function checkrunningDB(tx) {
tx.executeSql('SELECT max(id), sluttime FROM '+this.tabel, [], checkrunningSuccess, checkerrorCB);
}
function checkrunningSuccess(tx, results) {
if (results.rows.item(0).sluttime != null){
this.running = false;
} else{
this.running = true;
}
}
function checkerrorCB(err) {
this.running = false;
console.log(err);
}

Pass a callback function to CheckRunning:
CheckRunning(current[0], function(isRunning){...})
...
function CheckRunning(tabel, callback)
{
var isRunning = null; // we don't know yet
....
callback(isRunning);
}
It's similar to your tx.executeSql function which takes checkRunningSuccess as a callback. In that case the function name is hard coded as checkRunningSuccess, you could do the same in checkRunning.
By the way, if this is a public webapp running SQL queries makes you vulnerable to SQL injection attacks.

Related

Firebase array always return false

I'm creating an application with ionic and firebase. I'm trying to verify if a element exists in my array, and if it does, I need to return true, else I need to return false. The problem is, it always return false, even if the item exists in firebase. Can you please tell me what is going wrong with following code?
Here's my service:
function IsReserved(id){
var ref = fb.child('/reserved/').orderByChild('product').equalTo(id);
ref.once("value").then(function(snapshot){
snapshot.forEach(function(data){
if(data.val().user === $rootScope.currentUser.$id){
console.log(data.val().user + " * " + $rootScope.currentUser.$id);
return true;
}
});
});
return false;
}
Here is my controller:
function Reservar(produto) {
if(!$rootScope.cart){
$rootScope.cart = [];
$rootScope.fprice = 0;
}
var user=$rootScope.currentUser;
var res = vm.IsReserved(produto.$id);
console.log(res);
if(res){
console.log("já reservado");
return;
}
Here is my firebase strucure:
-reserved:
--KdS2cH1OJ5MhKAV6Yio:
-product: "product1"
-user: "W30BB1RMg1XhNo9og9cMo4Gpr4S2"
Your code won't work because firebase works asynchronously.
You should use a callback function as a parameter, something like this:
function IsReserved(id, callback){
var ref = fb.child('/reserved/').orderByChild('product').equalTo(id);
ref.once("value").then(function(snapshot){
snapshot.forEach(function(data){
if(data.val().user === $rootScope.currentUser.$id){
console.log(data.val().user + " * " + $rootScope.currentUser.$id);
callback(true);
return;
}
});
});
return false; //-- This will always be executed before the code inside the .then, that's why your function always returns false
}
And on you controller, something like this:
function Reservar(produto)
{
if(!$rootScope.cart){
$rootScope.cart = [];
$rootScope.fprice = 0;
}
var user=$rootScope.currentUser;
vm.IsReserved(produto.$id, function(response){
console.log(response);
if(response){
console.log("já reservado");
}
});
}
Could you understand?

function is running before sql query is finished, node js

i have made an event listener for my server which listens for when someone is trying to log on, so i do an sql query and then if the info matches, it logs them on. problem is, its testing if the info matches before it runs the query so it will always return false. here is my code
player.on('checkLogin', function(data)
{
var logIn = "";
connection.query({
sql: "SELECT * FROM users WHERE username = ?",
values: [data.user] },
function(error, results, fields)
{
if (error)
{
console.log(error);
}
if(results)
{
var pass = "";
for (i = 0; i < results.length; i++)
{
pass = (results[i].password);
}
var input = crypto.createHash('md5').update(data.pass).digest("hex");
if (pass == input)
{
logIn = true;
}
}
});
if (logIn == true)
{
this.em.emit('login',
{
id: player.playerid
});
}
}.bind(this));
I heard promises will fix this but is there any easy way around this?, thanks in advance
The issue you have is your if(login == true) is outside of the completion handlers of the query.
Move the if(logIn == true) into your completion handler.
player.on('checkLogin', function(data)
{
var logIn = "";
connection.query({
sql: "SELECT * FROM users WHERE username = ?",
values: [data.user] },
function(error, results, fields)
{
if (error)
{
console.log(error);
}
if(results)
{
var pass = "";
for (i = 0; i < results.length; i++)
{
pass = (results[i].password);
}
var input = crypto.createHash('md5').update(data.pass).digest("hex");
if (pass == input)
{
logIn = true; //<-- could just handle it here.
}
if (logIn == true) //<-- moved inside completion handler
{
//log on
}
}
});
}.bind(this));
Now the reason it is being called is your connection.query runs asynchronously which is why the function handler is used. Code outside the completion handler will be run immediately.
EDIT from Additional Changes
Based on your changes your this scope will be changed when moved inside the completion handler function. To get a reference back you will need a reference to the player scope (assuming that is what this is). So that can be handled simply by creating a variable of this as var that = this; at the top of your checklogin function. Something like:
player.on('checkLogin', function(data)
{
var that = this; //create a variable to store the scope (this)
var logIn = "";
connection.query({
sql: "SELECT * FROM users WHERE username = ?",
values: [data.user] },
function(error, results, fields)
{
if (error)
{
console.log(error);
}
if(results)
{
var pass = "";
for (i = 0; i < results.length; i++)
{
pass = (results[i].password);
}
var input = crypto.createHash('md5').update(data.pass).digest("hex");
if (pass == input)
{
logIn = true; //<-- could just handle it here.
}
if (logIn == true) //<-- moved inside completion handler
{
//log on
that.em.emit('login', //<-- reference `that` not `this`
{
id: player.playerid
});
}
}
});
}.bind(this));

How To handle result set of websql in html 5?

I m creating mobile web application using html5 and javascript.I m having two javascript files. AttributesDatabase.js and AttributeView.js.From AttributeView.js i m calling one function from AttributeDatabase.js in that i m executing one select query.Now the query result should go to AtttributeView.js.But the Websql transaction is asynchronous call that is what it is not returning proper result.Is there any way to handle the websql result.
Please help if any way there?
Edited
AttributeView.js
var AttributeDAOObj = new AttributeDAO();
AttributeDAOObj.GetAttributeList();
alert(AttributeDAOObj.GetAttributeList()); //This alert is coming as undefined.
AttributeDAO.js
this.GetAttributeList = function () {
var baseDAOObj = new BaseDAO();
var query = "SELECT AttributeName FROM LOGS";
// this.Successcalbackfromsrc = this.myInstance.Successcalback;
var parm = { 'query': query, 'Successcalback': this.myInstance.Successcalback };
baseDAOObj.executeSql(parm);
}
//To Create database and execute sql queries.
function BaseDAO() {
this.myInstance = this;
//Creating database
this.GetMobileWebDB = function () {
if (dbName == null) {
var dbName = 'ABC';
}
var objMobileWebDB = window.openDatabase(dbName, "1.0", dbName, 5 * 1024 * 1024);
return objMobileWebDB;
}
//Executing queries and getting result
this.executeSql = function (query) {
var objMobileWebDB = this.myInstance.GetMobileWebDB();
objMobileWebDB.transaction(function (transaction) {
//In this transaction i m returning the result.The result value is coming.
transaction.executeSql(query, [], function (transaction, result) { return result; }, this.Errorclback);
});
}
}
The problem is in you succes call back (like in the comment to your question, stated by DCoder)
function (transaction, result) { return result; }
this is returning where to?
So this is how to do it (or at least one way)
you can do for example:
function (transaction,result){
console.log("yes, I have some result, but this doesn't say anything, empty result gives also a result");
// so check if there is a result:
if (result != null && result.rows != null) {
if (result.rows.length == 0) {
// do something if there is no result
}else{
for ( var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
var id = result.rows.item(i).id; //supposing there is an id in your result
console.log('Yeah! row id = '+id);
}
}
}else{
// do something if there is no result
}
};
note the code above can be compacter, but this is how to understand it better.
another way is to put this function is a seperate piece of code, so you keep the sql statement more compact and readable. Like you call you error callback this can be in your function (with this. in front of it) or a completely seperate function.

How does jQuery do async:false in its $.ajax method?

I have a similar question here, but I thought I'd ask it a different way to cast a wider net. I haven't come across a workable solution yet (that I know of).
I'd like for XCode to issue a JavaScript command and get a return value back from an executeSql callback.
From the research that I've been reading, I can't issue a synchronous executeSql command. The closest I came was trying to Spin Lock until I got the callback. But that hasn't worked yet either. Maybe my spinning isn't giving the callback chance to come back (See code below).
Q: How can jQuery have an async=false argument when it comes to Ajax? Is there something different about XHR than there is about the executeSql command?
Here is my proof-of-concept so far: (Please don't laugh)
// First define any dom elements that are referenced more than once.
var dom = {};
dom.TestID = $('#TestID'); // <input id="TestID">
dom.msg = $('#msg'); // <div id="msg"></div>
window.dbo = openDatabase('POC','1.0','Proof-Of-Concept', 1024*1024); // 1MB
!function($, window, undefined) {
var Variables = {}; // Variables that are to be passed from one function to another.
Variables.Ready = new $.Deferred();
Variables.DropTableDeferred = new $.Deferred();
Variables.CreateTableDeferred = new $.Deferred();
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'drop table Test;',
[],
Variables.DropTableDeferred.resolve()
// ,WebSqlError
);
});
$.when(Variables.DropTableDeferred).done(function() {
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'CREATE TABLE IF NOT EXISTS Test'
+ '(TestID Integer NOT NULL PRIMARY KEY'
+ ',TestSort Int'
+ ');',
[],
Variables.CreateTableDeferred.resolve(),
WebSqlError
);
});
});
$.when(Variables.CreateTableDeferred).done(function() {
for (var i=0;i < 10;i++) {
myFunction(i);
};
Variables.Ready.resolve();
function myFunction(i) {
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'INSERT INTO Test(TestID,TestSort) VALUES(?,?)',
[
i
,i+100000
]
,function() {}
,WebSqlError
)
});
};
});
$.when(Variables.Ready).done(function() {
$('#Save').removeAttr('disabled');
});
}(jQuery, window);
!function($, window, undefined) {
var Variables = {};
$(document).on('click','#Save',function() {
var local = {};
local.result = barcode.Scan(dom.TestID.val());
console.log(local.result);
});
var mySuccess = function(transaction, argument) {
var local = {};
for (local.i=0; local.i < argument.rows.length; local.i++) {
local.qry = argument.rows.item(local.i);
Variables.result = local.qry.TestSort;
}
Variables.Return = true;
};
var myError = function(transaction, argument) {
dom.msg.text(argument.message);
Variables.result = '';
Variables.Return = true;
}
var barcode = {};
barcode.Scan = function(argument) {
var local = {};
Variables.result = '';
Variables.Return = false;
window.dbo.transaction(function(myTrans) {
myTrans.executeSql(
'SELECT * FROM Test WHERE TestID=?'
,[argument]
,mySuccess
,myError
)
});
for (local.I = 0;local.I < 3; local.I++) { // Try a bunch of times.
if (Variables.Return) break; // Gets set in mySuccess and myError
SpinLock(250);
}
return Variables.result;
}
var SpinLock = function(milliseconds) {
var local = {};
local.StartTime = Date.now();
do {
} while (Date.now() < local.StartTime + milliseconds);
}
function WebSqlError(tx,result) {
if (dom.msg.text()) {
dom.msg.append('<br>');
}
dom.msg.append(result.message);
}
}(jQuery, window);
Is there something different about XHR than there is about the executeSql command?
Kind of.
How can jQuery have an async=false argument when it comes to Ajax?
Ajax, or rather XMLHttpRequest, isn't strictly limited to being asynchronous -- though, as the original acronym suggested, it is preferred.
jQuery.ajax()'s async option is tied to the boolean async argument of xhr.open():
void open(
DOMString method,
DOMString url,
optional boolean async, // <---
optional DOMString user,
optional DOMString password
);
The Web SQL Database spec does also define a Synchronous database API. However, it's only available to implementations of the WorkerUtils interface, defined primarily for Web Workers:
window.dbo = openDatabaseSync('POC','1.0','Proof-Of-Concept', 1024*1024);
var results;
window.dbo.transaction(function (trans) {
results = trans.executeSql('...');
});
If the environment running the script hasn't implemented this interface, then you're stuck with the asynchronous API and returning the result will not be feasible. You can't force blocking/waiting of asynchronous tasks for the reason you suspected:
Maybe my spinning isn't giving the callback chance to come back (See code below).

I add 10 functions to a code, I don't even call any of them, but the code stops working!

Alone, this code works:
CustomButton = {
1: function () {
alert("Just testing")
},
}
I add the code below and the code above stops working:
function getvisitingnow() {
return document.location;
}
function getcontents(uri) {
var req = new XMLHttpRequest();
req.open('GET', uri, true);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200) {
return req.responseText;
}
}
};
req.send();
}
function regexforsitefound(uri, searchcontents) {
var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
var sitefound = searchcontents.match(re);
if (sitefound[0]) return sitefound[0] else return null;
}
function regexforcategoryfound(uri, searchcontents) {
var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
var categoryfound = searchcontents.match(re);
if (categoryfound[1]) return categoryfound[1] else return null;
}
function regexfordomainname(uri) {
var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
var domainname = uri.match(re);
if (domainname) return domainname;
}
function regexforparentdir(uri) {
var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
var parentdir = uri.match(re);
if (parentdir) return parentdir;
}
function getcomparisonlink(visitingnow) {
var searchuri = null;
var searchcontents = null;
var uri = visitingnow;
while(true) {
searchuri = 'http://www.google.com.br/search?';
searchuri += 'q='+ uri +'&btnG=Search+Directory&hl=en&cat=gwd%2FTop';
searchcontents = getcontents(searchuri);
var sitefound = regexforsitefound(searchcontents);
if (sitefound) {
var categoryfound = regexforcategoryfound(searchcontents);
if (categoryfound) {
return categoryfound;
}
} else {
var domainname = regexfordomainname(uri);
if (!domainname) {
var parentdir = regexforparentdir(uri);
uri = parentdir;
} else {
return null;
}
}
}
}
function clickedlink(event){
var visitingnow = getvisitingnow();
if (visitingnow) {
getcomparisonlink(visitingnow);
if (comparisonlink) {
tab.open(comparisonlink);
};
}
}
function createBookmarkItem() {
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
var item = document.createElementNS(XUL_NS, "toolbarbutton");
item.setAttribute("id", "Testing-Doit-Button2");
item.setAttribute("class", "bookmark-item pagerank");
item.setAttribute("tooltiptext", "Do it!");
item.setAttribute("oncommand", "testing_doit();");
return item;
}
function placeBookmarkItem() {
var toolbar = document.getElementById("PersonalToolbar");
var button = createBookmarkItem();
toolbar.appendChild(button);
}
Why?
try adding your functions one by one. see at which function your code stops working. then empty the function contents only to put it back with pieces at a time. check again where your code stops working. about there should be a syntax error.
But as Bobby suggests, the easier way is to try Firefox Errorlog, or maybe Firebug.
One little JavaScript-error can break a lot of things. You have forgotten to add semicolons in two places.
There needs to be a semicolon after sitefound[0] here:
function regexforsitefound(uri, searchcontents) {
var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*? ){2}\\</div\\>", "mi");
var sitefound = searchcontents.match(re);
if (sitefound[0]) return sitefound[0] else return null;
}
and one after categoryfound[1] here:
function regexforcategoryfound(uri, searchcontents) {
var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
var categoryfound = searchcontents.match(re);
if (categoryfound[1]) return categoryfound[1] else return null;
}
if (sitefound[0]) return sitefound[0] else return null;
This syntax is invalid.
Try:
if (sitefound[0])
return sitefound[0];
else
return null;
If you are a Mac user, open (a recent version) of Safari and hit
⌥⌘ + i,
which opens up a great panel with lots of charts and data about the client-server interaction. You can also see and locate javascript errors, or debug javascript in a console directly. neat.
For Firefox, try the excellent firebug to see, what went wrong where .. in their own words: [with Firebug] .. you can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.
The comma after the function in CustomButton can break code the code in IE. Also, if you are using CustomButton the first time here, you should introduce it with var. I know these are not the issues you asked for, but otherwise, everything seems correct.

Categories