#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141

    Help working around scope resolution in callbacks?


    I'm an experienced PHP developer coming to grips with Node.js.

    The following code illustrates a concept I'm struggling with. I know why it doesn't work, but I don't know how to refactor the code to solve the problem.

    Basically, I'm querying rows from a DB, then I have to merge that information into existing data stored in Redis. Because of the callback system, I don't know how to make the variables in one callback available to the next.

    javascript Code:
     
    function updateCache () {
        var cacheKey = 'resources';
     
        var qSel = "SELECT * FROM resources";
     
        db.query(qSel, function(err, rows) {
            for (var i = 0, len = rows.length; i < len; i++) {
                var row = rows[i];
                var cacheField = row['colA'];
     
                // We need to pull the existing resource and merge in the nested objects.
                cache.hget(cacheKey, cacheField, function(err, cachedResource) {
                    var pkg = JSON.parse(cachedResource);
     
                    /**
                     * PROBLEM: Here I need to manipulate {pkg} with the values from {row}, but {row} is out of scope.
                     */
     
                    /**
                     * PROBLEM: Then I need to set {pkg}} back in the cache, but {cacheField} is out of scope.
                     */
                    cache.hset(resPath, cacheField, JSON.stringify(pkg));
                });
            }
        });
    }


    I suspect I'll face-palm myself when I see how to work around this.
    Ooh, they have the Internet on computers now!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    You can call an anonymous function to pass the variables into the scope (untested):
    Code:
    cache.hget(cacheKey, cacheField, (function(row, cacheField) {
      return function(err, cachedResource) {
        var pkg = JSON.parse(cachedResource);
        row
        cacheField
        cache.hset(resPath, cacheField, JSON.stringify(pkg));
      };
    })(row, cacheField);

    Comments on this post

    • DrWorm agrees
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141
    Thanks!

    I figured out I could use a function to bring it into scope, and was mean to come back and post. But I didn't know how to do it with an anonymous function, so thank you very much.
    Ooh, they have the Internet on computers now!

IMN logo majestic logo threadwatch logo seochat tools logo