Accessing variable inside WebSQL Function - javascript

I am using the following code
var Year12=new Array();
GetYear(function(Year12)
{
alert(Year12);
});
function GetYear(callback)
{
var selectAllStatement = "SELECT DISTINCT year FROM mytable";
var db = openDatabase("Postit", "1.0", "Notebook", 200000);
var dataset;
alert("1");
db.transaction(function(tx) {
alert("2");
tx.executeSql(selectAllStatement, [], function(tx, result)
{
alert("3");
dataset = result.rows;
for (var i = 0, item = null; i < dataset.length; i++)
{
item = dataset.item(i);
Year12[i]=item['year'];
}
callback(Year12);
});
});
}
Here the tx.executeSql statements are not getting executed means alert 3 is not displaying.Is there any way to do this

The db.transaction and tx.executeSql calls are asynchronous, hence the callback functions. That means that GetYear will finish executing and return before the tx.executeSql callback will populate your Year12 array.
Once you have asynchronous behavior and callbacks, the only sensible solution is, of course, more callbacks. You need a structure more like this:
function GetYear(callback) {
//...
db.transaction(function(tx) {
//...
tx.executeSql(selectuniqueyearStatement, [], function(tx, result) {
var Year12 = [ ];
//... populate the locale Year12 using result.rows
callback(Year12);
});
});
}
and then use it like this:
GetYear(function(year12) {
// Do whatever you need to do with the year12 array...
});
Basically the same code structure and strategies that you use with AJAX calls.

Related

Mongoose - How to save results to an array

I have a mongodb query and I want to save the results to an array. I also define an empty array outside of query.
var dietaryResults = [];
for (var key in dietary){
Restaurant.find(
{ $text : { $search : dietary[key] } },
{ score : { $meta: "textScore" } }
).sort({ score : { $meta : 'textScore' } }).exec(function(err, results) {
for (var i in results){
dietaryResults.push(results[i]);
}
console.log(dietaryResults);
});
}
If I do console.log(dietaryResults) inside the query like above, I can see the results being pushed into the array. But if I put console.log(dietaryResults) outside of the array (which what I want), it prints an empty string. Can anyone explain this behavior and suggest a solution for this? Thanks.
When I use bluebird (which is a kind of Promise) it works.
var Promise = require('bluebird');
var mongoose = Promise.promisifyAll(require('mongoose'));
var promises = Restaurant.find({ $text : { $search : query } }, function(err, results) {
results.forEach(function(element) {
finalResults.push(element);
});
});
Promise.all(promises).then(function() {
console.log(finalResults);
}).error(console.error);
Restaurant.find is asynchronous.
Function insinde .exec part is being executed after loop ends.
Try
for (var key in dietary){
Restaurant.find(
{ $text : { $search : dietary[key] } },
{ score : { $meta: "textScore" } }
).sort({ score : { $meta : 'textScore' } }).exec(function(err, results) {
for (var i in results){
dietaryResults.push(results[i]);
}
console.log('added');
});
}
console.log('loop end');
You will be able to see, that 'loop end' log will be printed before 'added' log.
If you need all results in array, you should fill this array in callback. There is no way to get data synchronously.
For more information about callbacks and async functions check out this article: https://www.tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm
I'd suggest to search for all required data at once and avoid searching inside loop to have single callback and do everything you need with results inside that callback.
Smth like this may work. If not you should search for some way to get all data in one request
Restaurant.find(
{ $text : { $search : { $in: Object.values(dietary)} } },
{ score : { $meta: "textScore" } }
).sort({ score : { $meta : 'textScore' } }).exec(function(err, results) {
for (var i in results){
dietaryResults.push(results[i]);
}
//do stuff here;
});
You need callback or promise.You can use outside of this **Restaurant.find( **
this:setTimeOut(function s(){console.log(dietaryResults)},1000)
Because you need to learn async functions.
As of javascript asynchronicity fashion, You can't be sure that the .exec callback will run (and end) before the for loop end. (you can have a better understanding on this topic by reading about the event-loop)
This way, dietaryResults can be empty (depend on the query speed) in any other place.
You can read more about using promises with mongoose and node in order to be sure dietaryResults will be full with the results,
and also have better understanding on this topic by reading about Node event loop.
For now, I'm not fully sure this will work - but that's is your direction:
var query = {...} // (enter your query here)
var dietaryResults = [];
Object.entries(diatery).map(async key => {
const results = await Restaurant.find(query).sort();
results.map(result => dietaryResults.push(result));
});
console.log(dietaryResults);

Integrating asynchronous mongo call within an inner forEach loop

I got two loops, the outer loops over the users and the inner one loops over the venueID's of each user. Within the inner loop I want to look up the venue and attach it to an array defined in the outer look (userItem). However because forEach is synchronous and the mongo database look up is asynchronous the result always remains empty. I've tried to integrate this answer but to no avail. How to do this?
ret = [];
users.forEach(function(user) {
var userItem = user.getSanitised('ADM');
userItem.venues = [];
var tmp = [];
userItem.adminVenueIds.forEach(function(adminVenueId){
tmp.push(function(callback) {
Venue.findOne({_id:adminVenueId}, function(error, venue) {
callback(null, venue.toObject());
});
});
});
async.parallel(userItem.venues, function(err, result) {
/* this code will run after all calls finished the job or
when any of the calls passes an error */
if (err)
return console.log(err);
userItem.venues.push(result);
});
ret.push(userItem);
});
Tried the following as well but doesn't work also
users.forEach(function(user) {
var userItem = [];
async.series({
setUserItem : function(callback)
{
userItem = user.getSanitised('ADM');
callback(null, 'OK');
},
setUserVenues : function(callback)
{
userItem.venues = [];
user.adminVenueIds.forEach(function(adminVenueId,index) {
Venue.findOne({_id:adminVenueId}, function(error, venue) {
userItem.venues.push(venue.toObject());
if((index+1) == user.adminVenueIds.length)
callback(null, 'OK');
});
});
}
},
function(error, results) {
if(error)
winston.error(error);
ret.push(userItem);
}
);
});
You could simply put an if statement (in your case put the conditional as the array length) then when the loop is done you could then make it continue doing its thing by calling a function to continue (or put your code in there, but it will start to look messy)
var ret = [];
var test = [];
for (var i = 0; i < 20; i++) {
for (var x = 0; x < 20; x++) {
setTimeout(function() {
test.push("Test"+x);
if (x === 20) {
finishIt();
}
}, 300)
}
}
function finishIt() {
console.log(test);
ret.push(test);
}
I think you might want to look into using Mongoose. It is a NodeJS application layer on top of MongoDB that provides a more SQL like experience.
http://mongoosejs.com
I ended up with the following solution. It's dirty but I guess that's just nodejs being nodejs.
users.forEach(function(user) {
var userItem = user.getSanitised('ADM');
userItem.venues = [];
user.adminVenueIds.forEach(function(adminVenueId) {
Venue.findOne({_id:adminVenueId}, function(error, venue) {
userItem.venues.push(venue.toObject());
});
});
(function(){
if(userItem.venues.length == user.adminVenueIds.length) {
ret.push(userItem);
} else {
setTimeout(arguments.callee, 30);
}
})();
});

Add sqlite query result to angularjs $scope

I have this sqlite query in a angularjs app.
$scope.testArr = [];
$scope.testFunk = function(){
var db = window.openDatabase("test", "1.0", "Test DB", 1000000);
db.transaction(function(tx){
tx.executeSql('SELECT * FROM EVENTS',
[],
//Callback function with transaction and
//results objects
function(tx, results){
//Count the results rows
var rowsCount = results.rows.length;
//Loop the rows
for (var i = 0; i < rowsCount; i++){
//Build a results string, notice the
//column names are called
$scope.testArr.push({
id: results.rows.item(i).eventId,
nameSel: results.rows.item(i).eventNameSel,
name: results.rows.item(i).eventName,
eventDesc: results.rows.item(i).eventDesc,
eventUrl: results.rows.item(i).eventUrl,
time: results.rows.item(i).eventTime
})
}
console.log($scope.testArr);
},
errorHandler);
});
}
//
The console.log returns the right objects, but If I do a console.log outside of the db.transaction function it returns empty. The problem is that I want to call the $scope.testFunk() function and return the result from the query to a variable which I then can process further, like:
var result = $scope.testFunk();
console.log(result);
// prints results from query.
I suggest you to use a service.
The reason it's not working now is because when you trying to print the results, it's still not populated. you can add $watch over results and work with it when it's populated.
$scope.testFunk(); // modify $scope.results within the function
$scope.$watch('results', function(resultsNewVal, resultsOldVal) {
console.log(resultsNewVal);
}

getting values from sqlite database and insert into a array

I've made a function myFunction which performs simple task i.e. to retrieve data from sqlite database and save it in a array which can be used easily. This function should follow these steps to fulfill my functionality.
To retrieve data from database.
To save it into array.
return array
But it is performing these steps in this sequences; 3, 1, 2, so I am not getting the data because it returns without getting it.
e.g.
function myFunction() {
var ret;
var arr = [];
db.transaction(function (trans) {
trans.executeSql('SELECT * FROM tblname', [],
function (transaction, result) {
if (result != null && result.rows != null) {
for (var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
ret = row.urlcolmn;
arr.push(ret);
alert('alert 1'); // just to check which line(alert) is called first
}
}
}, errorHandler);
}, errorHandler, nullHandler);
alert('alert 2'); // just to check which line(alert) is called first
return arr;
}
In above code alert 2 is shown before alert 1..... that's why it doesn't return value in array. This problem can be because sqlite in JS is Asynchronous.

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.

Categories