0

I am having a weird error and I've spent my whole day just to figure it out my self but I give up :) The code below is in a function that gets data from an SQLite database and assign the results in a multidimensional array then the function returns this array. This actually works but when the rows from the database is 3 and above the error below shows. I'm sure the assigned values for each is consistent throughout the loop because I tried to use typeof to know the type. Please help me. Thanks!

TypeError: Type error

 var db = openDB();
 var arrItems = [[],[]];

 db.transaction(function(tx) {
    var rs = tx.executeSql('SELECT * FROM items WHERE category=?;', [txtcategory]);
    arrItems.length = rs.rows.length;

   for(var i = 0; i < rs.rows.length; i++) {

      arrItems[i][0] = rs.rows.item(i).category;
      arrItems[i][1] = rs.rows.item(i).date;
      arrItems[i][2] = rs.rows.item(i).descr;
      arrItems[i][3] = rs.rows.item(i).descrlong;
      arrItems[i][4] = rs.rows.item(i).value;
      }

}

1 Answer 1

2

Your multidimensional array (really just an array of arrays) is declared to only have two rows:

var arrItems = [[],[]];

This is why it fails when you have 3 or more rows. Rather than declare it statically, you should probably add a new row within your loop. So

var arrItems = [];

And then later, something like

arrItems[i] = []

Depending on what you're doing, you might want to first check whether the row already exists.

Sign up to request clarification or add additional context in comments.

1 Comment

ah I thought what I did was to declare a two dimensional array with no specified length yet.Sorry I'm new with javascript.Can you elaborate more your suggestion? How can i add a new row to the array? thanks UPDATE: Nevermind I got it now. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.