SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add a third variable to a WEbSQL callback function

    This is the code. I tried exploring the options of SQL for solving this, but they are limited. I want to pass newSync[i].id to the onsuccess callback function, but I fail
    Code:
       tx.executeSql('SELECT * FROM bookmarks WHERE bookmarkID = ?', [newSync[i].id],
              function(tx,results){
                console.log('results.rows.item(0).bookmarkID', results.rows.item(0).bookmarkID);
                tx.executeSql('UPDATE bookmarksSync SET thumbnail=?, ts_created=?, visits=?, visits_morning=?, visits_afternoon=?, visits_evening=?, visits_night=?, position=?, idgroup=? WHERE bookmarkID=?', 
              [results.rows.item(0).thumbnail, results.rows.item(0).ts_created, results.rows.item(0).visits, results.rows.item(0).visits_morning, results.rows.item(0).visits_afternoon, results.rows.item(0).visits_evening, results.rows.item(0).visits_night, results.rows.item(0).position, 0, newSync[i].id]],
              null,speeddial.storage.onError)
              }
              , speeddial.storage.onError);
    newSync[i].id appears to be undefined and I am pretty sure I have to pass it to the callback function, but I dont know how... Any ideas? I want to be able to pass the newSync[i].id and the results from the SQL selection to another function, which will update the WebSQL table.
    I fail even if I add newSync[i].id as a third argument to function(tx,results)

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,335
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    If newSync is not defined globally it will be out of scope when passed as a parameter to the 'executeSql' call.
    Where/how/when is newSync defined elsewhere in your code?
    How do you get the value for 'i' ?; which is also out of scope according to this code snippet.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both are defined inside this function, they are not global. I will post the whole code. This is a part of an chrome extension. My bigger problem is , that function(tx,results){...} is a callback function and should be executed after all code is executed( and the cycle for i is run. So I want to pass the current value to this function...

    here is the whole function

    Code:
    speeddial.storage.Sync = function() {
      chrome.bookmarks.getChildren(String(localStorage['rootFolderID']), function(newSync){
        speeddial.storage.db.transaction(function(tx){
          tx.executeSql('DELETE FROM bookmarksSync',null,null,speeddial.storage.onError);	
          tx.executeSql('DELETE FROM groupsSync',null,null,speeddial.storage.onError);	
          for (var i=0; i<newSync.length; i++){
    	if(!newSync[i].url)
    	{
    // 	  tx.executeSql('SELECT * INTO groupsSync FROM groups', [],null
    // // // 			function(tx,results){
    // // 
    // // // 	    console.log('results', results);
    // // 	    
    // // // 	    speeddial.storage.SyncGetChildren(newSync[i].id)
    // // // 	    speeddial.storage.SyncGetTry(newSync[i].id)
    // // 	    
    // // // 	  }
    // // 	    
    // 	  ,speeddial.storage.onError);
    	}
    	else
    	{
    	  tx.executeSql('INSERT INTO bookmarksSync (title, bookmarkID, url, idgroup) values (?, ?, ?, ?)', [newSync[i].title,  newSync[i].id, newSync[i].url, 0],null,speeddial.storage.onError);
    
    	      tx.executeSql('SELECT * FROM bookmarks WHERE bookmarkID = ?', [newSync[i].id], function(tx,results){
                console.log('results.rows.item(0).bookmarkID', results.rows.item(0).bookmarkID);
                tx.executeSql('UPDATE bookmarksSync SET thumbnail=?, ts_created=?, visits=?, visits_morning=?, visits_afternoon=?, visits_evening=?, visits_night=?, position=?, idgroup=? WHERE bookmarkID=?', 
              [results.rows.item(0).thumbnail, results.rows.item(0).ts_created, results.rows.item(0).visits, results.rows.item(0).visits_morning, results.rows.item(0).visits_afternoon, results.rows.item(0).visits_evening, results.rows.item(0).visits_night, results.rows.item(0).position, 0, newSync[i].id]],
              null,speeddial.storage.onError)
              }		  
    		  , speeddial.storage.onError);
    
    	}
    	//above is the end of else statement
          }
    //        	  tx.executeSql('DELETE FROM bookmarks',null,null,speeddial.storage.onError);
    // 	  tx.executeSql('INSERT INTO bookmarks SELECT * FROM bookmarksSync', [],null,speeddial.storage.onError); 
        })
      })	
    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •