在将记录添加到存储区后,我可以验证它是否存在,但是当我在另一个函数中声明一个新变量指向同一个数据库并存储和执行一个查询时,我会得到以下错误消息。
存储: null不存在。
我用来检索数据的方法被复制到我要添加记录的位置,只是为了确保它的语法正确,并且在那里工作。但是当我在另一个函数中时,它就找不到数据了。
以下是一些代码:
我的目标是跟踪缓存数据的时间,以便在数据过期时重新获取数据。"updateCacheAge“方法似乎有效。record对象填充了我期望的JSON对象。
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存储的数据不存在,错误消息似乎表明存储本身不存在。
我在这里做傻事吗?
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;
};发布于 2014-08-13 20:51:13
我找到了敌人,敌人就是我。
我最初对这个库的实验起了作用,我把这个实验的残余留在了我的模块的根部。我很清楚,但我忘了这件事,花了我几个小时的时间。是我的错。
我正在为我的YDN数据库重用一个"db“名称,而这些都是矛盾的。不过,JavaScript并没有向我抱怨。一旦我评论了我所有的初步实验,一切都开始像预期的那样工作。
https://stackoverflow.com/questions/25289906
复制相似问题