0

我正在尝试构建一个函数,在传递 keypath 之后检索商店中的下一个元素。我的 getItem 看起来像这样。

req = smartpigsdb.get(store, keypath);
req.done(function(record) { 
    if(record!==undefined || typeof record=='object'){
    // do something with the record
}
else console.log('error getItem: ' + e);
});
req.fail(function(e) {
    console.log(e);
});

如何使用 YDN-DB 实现这一目标?

4

2 回答 2

1

如果您不知道密钥,则无法使用get. 您必须使用values, 来迭代记录。限制为 1,这样您就只能得到一个结果。您可以使用您的已知密钥的 lowerBound keyRange,这样您将在密钥之后获得下一个对象。

于 2014-08-28T22:12:40.070 回答
0

最后我做了这样的事情,它有效,但我不确定是最好的方法。'idx' 是存储对象存储的索引。

var range = new ydn.db.KeyRange.lowerBound(index+1);
var iter = new ydn.db.Iterator(store, 'idx', range); // 'idx' is an index for store object store
db.values(iter, 1).done(function(item) {
    if(item.length > 0){
        var req = db.get(store, item[0]);
        req.done(function(record) {
            if(record!==undefined || typeof record=='object'){
                // do soemthing with the record 
            }
            else {
                console.log('record could not be found');
            }
        });
        req.fail(function(e) {
            console.log(e);
        });
    }
});

如果我想检索以前的记录,我尝试了同样的事情range = new ydn.db.KeyRange.upperBound(index);,但我总是从商店获得第一条记录,所以我改用range = new ydn.db.KeyRange.lowerBound(index-1);

请给我一些反馈。再次感谢图书馆。问候,弗洛林。

于 2014-08-29T11:42:59.017 回答