Reputation: 91
Below is my indexeddb(IDB) objectStore name and its calling method.
What I want is get the values from objectStore and based on condition matched values add those values calculation into Variable.
var item_id = [];
var db;
function displayNotes(filter) {
var line = 0;
var transaction = db.transaction(["frp_item_master"], "readonly");
var itemsname =[];
var itemsqty =[];
var itemsprice =[];
var itemsdisc =[];
var itemstax =[];
var handleResult = function(event) {
var cursor = event.target.result;
var price_with_tax;
var i = 0;
i++;
if (cursor) {
++line;
if (cursor.value.item_id == 20008)
{
item_id.push(event.target.result.value.item_id);
//and other array push here
//call function
price_with_tax = (get_tax(itemstax)/100)*itemsprice;
}
cursor.continue();
}
else {
//error
}
};
Then after creating my function which is called in above method.
function get_tax(p_tax_master_id){
var get_me;
var total_tax_rate = 0;
var transaction = db.transaction(["frp_tax_master"], "readonly");
var objectStore = transaction.objectStore('frp_tax_master');
objectStore.openCursor().onsuccess = function(event){
var cur = event.target.result;
get_me = event.target.result.value.tax_master_id;
console.log('get_me'+get_me);
if (cur) {
if (get_me == 2008)
{
total_tax_rate += event.target.result.value.rate;
console.log("total tax"+total_tax_rate);
}
cur.continue();
}
else {
console.log('else'+get_me);
}
};
return total_tax_rate;
};
I am getting errors as shown in images. Cursor is still running even if there no value into the object store and it shows Value can not be set to null.
Can we just loop through all records and till last values are fetched then exit from cursor assign that values to the variable.
Basically I am adding some number to a variable. screenshot-1 screenshot-2
Upvotes: 1
Views: 1524
Reputation: 11
In addition to what Joshua said, the onsuccess
method is asynchronous, so your get_tax
function will always return 0.
Consider something like:
function get_tax(p_tax_master_id, cb) {
var get_me;
var total_tax_rate = 0;
var transaction = db.transaction(["frp_tax_master"], "readonly");
var objectStore = transaction.objectStore('frp_tax_master');
objectStore.openCursor().onsuccess = function(event){
var cur = event.target.result;
console.log('get_me'+get_me);
if (cur) {
get_me = event.target.result.value.tax_master_id;
if (get_me == 2008) {
total_tax_rate += event.target.result.value.rate;
console.log("total tax"+total_tax_rate);
}
cur.continue();
} else {
console.log('else'+get_me);
cb(null, total_tax_rate);
}
};
};
You might also prefer to use a library such as ZangoDB so you could perform a query like:
var db = new zango.Db('db_name', ['frp_tax_master']);
var frp_tax_master = db.collection('frp_tax_master');
function get_tax(p_tax_master_id, cb) {
frp_tax_master.find({
tax_master_id: 2008
}).group({
total_tax_rate: { $sum: '$rate' }
}).toArray((error, docs) => {
if (error || !docs[0]) { cb(error); }
else { cb(null, docs[0].total_tax_rate); }
});
}
Upvotes: 0
Reputation: 8365
In this line you get the cursor:
var cur = event.target.result;
And here you correctly check that the cursor is not null before using it:
if (cur) {
But this line assumes that event.target.result
is not null:
get_me = event.target.result.value.tax_master_id;
So when the cursor hits the end of its range, event.target.result
is null which gives you the "can't read property 'value' of null" error on the exact line the console is telling you.
Consider moving that line into the if (cur)
block.
Upvotes: 1