首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >YDN-DB not : null不存在

YDN-DB not : null不存在
EN

Stack Overflow用户
提问于 2014-08-13 15:14:20
回答 1查看 116关注 0票数 2

在将记录添加到存储区后,我可以验证它是否存在,但是当我在另一个函数中声明一个新变量指向同一个数据库并存储和执行一个查询时,我会得到以下错误消息。

存储: null不存在。

我用来检索数据的方法被复制到我要添加记录的位置,只是为了确保它的语法正确,并且在那里工作。但是当我在另一个函数中时,它就找不到数据了。

以下是一些代码:

我的目标是跟踪缓存数据的时间,以便在数据过期时重新获取数据。"updateCacheAge“方法似乎有效。record对象填充了我期望的JSON对象。

代码语言:javascript
复制
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.');
}

这是isStale函数。我第一次认为这可能会失败,因为缓存中没有任何东西。当发生这种情况时,我知道如何获取数据,然后更新cacheAge,以防止我回到数据库,直到缓存失效。

我确信我的代码可以改进,但这是我第一次通过解决这个问题。我遇到的问题是,我刚刚保存到mashCacheAge存储的数据不存在,错误消息似乎表明存储本身不存在。

我在这里做傻事吗?

代码语言:javascript
复制
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;
};
EN

回答 1

Stack Overflow用户

发布于 2014-08-13 20:51:13

我找到了敌人,敌人就是我。

我最初对这个库的实验起了作用,我把这个实验的残余留在了我的模块的根部。我很清楚,但我忘了这件事,花了我几个小时的时间。是我的错。

我正在为我的YDN数据库重用一个"db“名称,而这些都是矛盾的。不过,JavaScript并没有向我抱怨。一旦我评论了我所有的初步实验,一切都开始像预期的那样工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25289906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档