0

What is the simplest, clearest way of seeing the output of console.log events in code. I am creating an sqlite database and want to simply alert or console.log to test if it exists or not. For example, if I do this:

function onDeviceReady() {
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 5000);
    db.transaction(populateDB, errorCB, successCB);
    if(db)
    {
        console.log("DB  created");
    alert('The database is working');
    }
    else
    {
        alert('There is a problem');
    }
}

I am getting no alert. Doing something wrong?

I now have this code based on this: deviceReady not working in PhoneGap application, how to?

         <body onload="onDeviceReady()">

    function init(){
    document.addEventListener("deviceready", onDeviceReady, false);
    }
   // PhoneGap is ready
   //
   function onDeviceReady() {
    var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 5000);
    if(db) {
console.log('The database is working');
db.transaction(populateDB, errorCB, successCB); // only do stuff if db exists
 }
  else{
console.log('There is a problem');
    }
}                                                                                                             

but still no logs!

Community
  • 1
  • 1
Inkers
  • 219
  • 7
  • 27

1 Answers1

1

The console output can be seen in the dev-tools of your browser, which can be opened with F12 on IE, FF and Chrome. After opening, switch to the Console tab. All output from console.* will be visible there.

alert() or other popups (promt / confirm) can be problematic, because it stalls your entire javascript execution until the User confirms the dialog.

Most likely you are getting no alert, because the javascript already dies on this line:

db.transaction(populateDB, errorCB, successCB);

It should be:

if(db) {
    console.log('The database is working');
    db.transaction(populateDB, errorCB, successCB); // only do stuff if db exists
}
else{
    console.log('There is a problem');
}
Christoph
  • 50,121
  • 21
  • 99
  • 128
  • Thanks. I probably should have said that I am using Chrome. The console tab is just showing nothing. The code is exactly as it appears in the Cordova documentation. http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html – Inkers Jan 07 '13 at 10:44
  • then most likely your function does not get executed. Set a breakpoint into that function and check if the debugger runs into it. To do so, switch to the sources tab, select the correct file and click on the line-number in question to set a breakpoint. – Christoph Jan 07 '13 at 10:51
  • I tried that and no joy. Nothing showing under any of the storage in resources tab either. OK, will try he breakpoint. – Inkers Jan 07 '13 at 10:55
  • So, I set a breakpoint on that function, refreshed and nothing, although a slight problem with a different function appeared. When I hover no issues appear. – Inkers Jan 07 '13 at 11:10
  • Might the problem be with this document.addEventListener("deviceready", onDeviceReady, false); – Inkers Jan 07 '13 at 11:18
  • After I changed the code above and refreshed it worked. Happy days! Thanks. – Inkers Jan 07 '13 at 12:01