knadh / localStorageDB

A simple database layer for localStorage and sessionStorage for creating structured data in the form of databases and tables
http://nadh.in/code/localstoragedb
814 stars 128 forks source link

Not proper database taking during queryAll #66

Closed changtung closed 8 years ago

changtung commented 8 years ago

I do queryAll for table A and it returns table B data. i have tables A,B,C. table B has foreign key . Is this something with copying by reference or by value? queryAll('A',{ query: {var_id:n.id}); - returns B rows queryAll('A',{}); - returns proper A rows. Is this bug? Version 2.3.1

knadh commented 8 years ago

Please show more of your code.

changtung commented 8 years ago
function fillB(d) {
      for ( var i = 0 ; i < d.length ; i++ )
      {
          var user_id = d[i].user_id;
          var u_arr = lib.queryAll("users",{
            query: {ID:user_id}
          });

          d[i] = u_arr[0];
          $log.debug('ref1,reef2',d[i],u_arr[0]);
      }

      return d;
    }

first code somehow is called, but i assume later. here is declaration:

lib.createTable("d", ["user_id","pn","p"]);
    lib.createTable("users", [
    "bn",
    "c"
(...10 more)
]];

and finally place where it happens:

var user = lib.queryAll("users",{
  query: { username:user_info.username}
});

var d_arr2 = lib.queryAll("d",{
  query: { user_id: '3'}
});

$log.debug('d(1):',d_arr2);

var t = lib.queryAll("d",{
  query: function(row) {
            if(row.user_id.toString() === user[0].ID.toString()) {
                return true;
            } else {
                return false;
        }
    }
});
$log.debug('d(2):',t);

var t3 = lib.queryAll("d",{
});
$log.debug('d(3):',t3);

It's Angular 1.5.0 app and Google Chrome. debugs d(1) and d(2) have users tables, but d(3) has d tables records( correct ).

I suppose it's something related to keeping in a memory pool until refresh, because i am writing to a variable a user variable and it may preserve.

changtung commented 8 years ago

Problem was here:

d[i] = u_arr[0];

If i change to less good:

d[i].var1 = u_arr[0].var1;

Then it works.