Reputation: 349
I want to print the value of variable "set" that I have set inside the cursor, outside my cursor objct.
request.onsuccess = function(e){
var set = 0;
var transaction = db.transaction(['List'], "readonly");
var objectStore = transaction.objectStore('List');
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
// console.log(cursor.value.Name);
if (cursor.value.Name == $('#card').val())
{
console.log("aisa kabhi hoga hi nahi");
set = 1;
}
cursor.continue();
}
else
{
console.log('Entries all displayed.');
if (set == 0)
{
set= ippp();
console.log(set);
}
}
};
console.log(set);
}
When I print my set variable inside the cursor as shown "right data is printed".
But when I try to print the data of variable "set" outside cursor the value that I declared initially gets printed. "How is it possible as I am resetting the value of variable in my cursor"
My question is how can I access the value that I have set to variable "set" inside the cursor, outside of cursor object.
Upvotes: 1
Views: 223
Reputation: 18690
You need to learn about asynchronous programming before using indexedDB. The real answer is to go and learn this.
However, as a quick answer, you can use a callback function.
function outerFunction(myCallbackFunction) {
// do stuff
request.onsuccess = function(event) {
var cursor = event.target.value;
var value = cursor.value;
// Here is the whole trick to getting the value out. Pass the value to the
// callback function.
myCallbackFunction(value);
};
}
// Then to call it, you do something like this, where 'oncompleted'
// is the name of the callback function
outerFunction(function oncompleted(value) {
console.log('The value is', value);
});
Upvotes: 1