Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何从IndexedDb对象存储中获取多个键值_Angularjs_Indexeddb - Fatal编程技术网

Angularjs 如何从IndexedDb对象存储中获取多个键值

Angularjs 如何从IndexedDb对象存储中获取多个键值,angularjs,indexeddb,Angularjs,Indexeddb,有人能建议如何从IndexedDB objectstore获取多个键值吗?我做了多个选择,但一个都没用。 在这里创建indexedDb $provide.value('dbModel', { name: 'TestDB', version: '1', instance: {}, objectStoreName: { dashboards: 'Appointment' }, up

有人能建议如何从IndexedDB objectstore获取多个键值吗?我做了多个选择,但一个都没用。 在这里创建indexedDb

$provide.value('dbModel', {
        name: 'TestDB',
        version: '1',
        instance: {},
        objectStoreName: {
            dashboards: 'Appointment'
        },
        upgrade: function (e) {
            var db = e.target.result;
            if (!db.objectStoreNames.contains('Appointment')) {
                var calendarobj = db.createObjectStore('Appointment', {
                    keyPath: 'AppointmentId'
                });
                calendarobj.createIndex("AppointmentId", "AppointmentId", { multiEntry: true });
            }
        }
    });
数据看起来像

KeyPath-AppointmentId             Value
=====================             =====
1                                 Test 1
2                                 Test 2
3                                 Test 3
我有getObjectStore方法来获取objectstorename实例

getObjectStore: function (objectStoreName, mode) {
            var modeact = mode || _db.transactionTypes.readonly;
            var txn = _db.instance.transaction(objectStoreName, modeact);
            var store = txn.objectStore(objectStoreName);

            return store;
        }


var keys = [1,2]; // Want to get the First two record which has value 'Test 1' and 'Test 2'
var store = _db.getObjectStore(objectStoreName);
var tagIndex = store.index(store.keyPath); //AppointmentId

var request = tagIndex.openCursor(IDBKeyRange.only(keys));
//var request = tagIndex.get(keys);

request.onsuccess = function (event) {
                console.log(event.result);
}

像这样使用
[1,2]
是行不通的-这是一个恰好是一个包含两个成员的数组的键。索引数据库目前无法理解如何使用列表或键集进行查询

你有几个选择:

1-并行发出两个get请求。(因为订单是有保证的,所以第二个请求将在第一个请求之后完成,并且您知道两个结果都已返回。)

2-使用光标和范围:

var results = [];
store.openCursor(IDBKeyRange.bound(1, 2)).onsuccess = function(e) {
  var cursor = e.target.result;
  if (cursor) {
    results.push(cursor.value);
    cursor.continue();
  } else {
    // results will have the values
  }
};
3-在范围内使用getAll(仅限较新的浏览器-如果不可用,则返回光标)


请注意,在使用范围的选项2和选项3中,如果存在键为
1.5
的记录,您也会得到该记录。

可以使用复制,但我无权报告复制,因此我会再次发布答案。希望能有帮助

使用
/[store]:对象存储
//[键]:键的数组
//return:键值对的对象
const get=(存储,键)=>
我保证(
keys.map(
(键)=>
新承诺((解决、拒绝)=>{
const request=store.get(key);
request.onsuccess=({target:{result}})=>resolve(result);
request.onerror=({target:{error}})=>拒绝(error);
})
)
)。然后((值)=>
减少(
(结果、键、索引)=>((结果[键]=值[索引]),结果),
{}
)
);
例子
const request=indexedDB.open(“测试”);
request.onupgradeneeded=({target:{result:db}})=>{
db.createObjectStore(“设置”).transaction.oncomplete=({
目标:{db},
}) => {
常数存储=db
.事务(“设置”、“读写”)
.objectStore(“设置”);
添加(123,“foo”);
添加(456“巴”);
};
};
request.onsuccess=({target:{result:db}})=>{
获取(db.transaction(“设置”).objectStore(“设置”)[
“福”,
“酒吧”,
“buz”,
]).then(console.log);//{foo:123,bar:456,buz:undefined}
};

请在否决投票时添加评论。这样我可以改进我的问题。
var results = [];
store.openCursor(IDBKeyRange.bound(1, 2)).onsuccess = function(e) {
  var cursor = e.target.result;
  if (cursor) {
    results.push(cursor.value);
    cursor.continue();
  } else {
    // results will have the values
  }
};
store.getAll(IDBKeyRange.bound(1, 2)).onsuccess = function(e) {
  // e.target.result will have the entries
};