2

I can verify a record exists after adding it to a store but when I'm in another function and declare a new variable to point to the same database and store and execute a query I get the following error message.

Store: null not exists.

The method I'm using to retrieve the data was copied to the location where I'm adding the record just to make sure it's syntax was right and it works there. But when I'm in this other function it no longer finds the data.

Here is some code:

My goal is to keep track of when I cached data so that I can refetch data when it goes stale. The "updateCacheAge" method seems to work. The record object is populated with the JSON object I would expect.

updateCacheAge = function (dbName, cacheName) {
    // updates the mashCacheAge.  This helps track how old/stale a cache is.

    var cacheJSON = {
        id: cacheName,
        updatedDate: new Date().getTime()
    };

    mashDB = new ydn.db.Storage(dbName);

    mashDB.put({ name: 'mashCacheAge', keyPath: 'id' }, cacheJSON).done(function (key) {
        mashDB.executeSql('SELECT * FROM mashCacheAge WHERE id = \'' + cacheName + '\'').then(function (record) {
            $log.log('mashCacheAge record for: ' + cacheName);
            $log.log(record);
        });
    });


    $log.log(cacheName + ': mashCache was re-created.');
}

This is the isStale function. The first time through I would expect this to potentially fail because nothing is in cache. When that occurs I know to fetch my data then update the cacheAge to prevent me from going back to the database until the cache is stale.

I'm sure my code can be improved but this was my first pass at solving the problem. The problem I'm having is the data I just saved to the mashCacheAge store is not there and the error message seems to indicate the store itself is not there.

Am I doing something silly here?

isStale = function (dbName, cacheName, minutes) {
    // db: is 'mashCache'
    // store: No store is provided but might be added later to remove constaint 'mashCacheAge'
    // subject: is the name of the cache being evaluated.
    // minutes: is the number of minutes before the cache is considered stale.

    var deferred = $q.defer();
    var result = true;

    // get milliseconds version of minutes.
    var ageToleranceMilliseconds = (minutes * 60) * 1000;
    var currentDateMilliseconds = new Date().getTime();

    isStaleMashDB = new ydn.db.Storage(dbName);

    try {
        isStaleMashDB.executeSql('SELECT * FROM mashCacheAge WHERE id = \'' + cacheName + '\'').then(function (record) {
            $log.log('mashCacheAge record for: ' + cacheName);
            $log.log(record);



            var durationMilliseconds = currentDateMilliseconds - record[0].updatedDate;

            // Check if the data is stale.
            if (durationMilliseconds > ageToleranceMilliseconds) {
                result = true;
            }
            else { result = false; }

            deferred.resolve(result);
        });
    }
    catch (e) { deferred.resolve(true); }
    return deferred.promise;
};
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3448990
  • 323
  • 6
  • 15
  • One suggestion: you should use database schema for production site. See http://dev.yathit.com/ydn-db/getting-started.html#h2-schema – Kyaw Tun Aug 14 '14 at 02:59

1 Answers1

1

I located the enemy and the enemy is me.

My initial experimentation with this library worked and I left remnants of that experiment in the root of my module. I know better but it slipped my mind and cost me a couple hours of work. My bad.

I was reusing a "db" name for my YDN database and these conflicted. JavaScript didn't complain to me though. Once I commented out all my initial experimentation everything started working as expected.

user3448990
  • 323
  • 6
  • 15