Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Data From Object Not Working in Ionic

I've attached $cordovaSQLite to my ionic app. Here is a basic example of the code.

function retrieve() {
var q = $q.defer();

var query = 'SELECT user_credentials, user_id FROM Users;';

$ionicPlatform.ready(function (){
  $cordovaSQLite.execute(db, query).then(function(res) {
    if (res.rows.length > 0) {
      console.log("found user");
      console.log(res);
      console.log(JSON.stringify(res));
      q.resolve(res.rows[0]);
    } else {
      console.log("no rows found");
      q.resolve(false);

    }
  }, function (err) {
    q.reject(err);
  });
});

return q.promise;
}

Here is the code to open up the db.

if(window.cordova) {
  // App syntax
  db = $cordovaSQLite.openDB( "CoolApp.db" );
} else {
  // Ionic serve syntax
  db = window.openDatabase("CoolApp.db", "1.0", "Cool App", -1);
}

When I test my app on Chrome, my logs show this

rows: SQLResultSetRowList
  0: Object
    user_credentials: "asdf"
    user_id: 234
  length: 1
rowsAffected: 0

However when I view the logs when running on my iOS app or Safari, I receive

{"rows":{"length":1},"rowsAffected":0,"insertId":1}

My question is why am I not receiving the value of rows? Why does this work on the browser but not on iOS?

like image 972
thank_you Avatar asked Jul 27 '15 18:07

thank_you


2 Answers

Did you try to instantiate a webSQL instead of SQLite DB for your browser?

You can always just go back to SQLite for your device, but modern browsers like Chrome and Firefox don't support SQLite.

like image 26
Abhishek Jain Avatar answered Nov 09 '22 13:11

Abhishek Jain


You can get the results by querying the rows.item method with the corresponding index if multiple results were returned.

var elements = [];
for (var i = 0; i < result.rows.length; i++) {
    elements.push(result.rows.item(i));
}
return elements;

Where result is the object returned by $cordovaSQL when its promise is complete.

like image 60
radyz Avatar answered Nov 09 '22 14:11

radyz