IndexedDB 在JS中嵌套时-不再定义“while loop”变量

wi3ka0sx  于 11个月前  发布在  IndexedDB
关注(0)|答案(1)|浏览(162)

我的JS脚本检查,如果数据库有值,如果没有,它将填充数据库。当再次触发时,数据库中的值将显示在页面上。当我通过重新加载页面运行脚本两次时,这会起作用。当我在while循环中运行整个事情时,它根本不起作用。循环永远运行,因为我猜它不能进入marmaInfo.onsuccess = function(evt) {我不确定它是否是一个作用域问题,我只是不能弄清楚,为什么它的行为不同时,while循环是活跃的。

var key = $('#marmaID').val();
console.log("key " + key );
store = getObjectStore("marmasStore", 'readwrite');

let i = 0;
//while (i < 1) {
    var marmaInfo = store.get(key);
    marmaInfo.onsuccess = function(evt) {
        var req = store.openCursor(key);
        var record = evt.target.result;
        console.log("record:", record);
        req.onsuccess = function(e) {
          var cursor = e.target.result; 
          if (cursor !== null) { // key already exist
            console.log(key + " FOUND");
            console.log("load and display values of " + key);
            data = marmaInfo.result
            //build page
            // Marma information
            document.getElementById("headline").innerHTML = data.sanskrit;
            console.log("display values of " + key + " DONE");
            i++;
          } else { // key not exist
            console.log(key + " NOT FOUND");
            console.log("initial filling DB ...");
            initialMarmaData.forEach((marma) => {
                var request = store.put(marma);
                request.onsuccess = (event) => {
                    console.log(event.target.result + " initial filling DB DONE");
                };
            //return;
            });
            //store.add(obj)
          }
        };
    };
    marmaInfo.onerror = function(evt) {
        console.log("error");
        i++;    
    }
//}

字符串

lskq00tm

lskq00tm1#

@Barmar的想法是正确的,它适用于递归。非常感谢@Barmar
这是一个递归的代码:

let i = null;
function recurse() {
    i++;
    console.log("loop " + i);
    var key = $('#marmaID').val();
    console.log("key " + key );
    store = getObjectStore("marmasStore", 'readwrite');
        var marmaInfo = store.get(key);
        marmaInfo.onsuccess = function(evt) {
            var req = store.openCursor(key);
            var record = evt.target.result;
            req.onsuccess = function(e) {
                var cursor = e.target.result; 
                  if (cursor !== null) { // key already exist
                    console.log(key + " FOUND");
                    data = marmaInfo.result
                    //build page
                    document.getElementById("headline").innerHTML = data.sanskrit;
                    console.log("display values of " + key + " DONE");
                  } else { // key not exist
                    console.log(key + " NOT FOUND");
                    console.log("initial filling DB ...");
                    initialMarmaData.forEach((marma) => {
                        var request = store.put(marma);
                        request.onsuccess = (event) => {
                            console.log(event.target.result + " initial filling DB DONE");
                        };
                    });
                      if (i > 2) {
                        return;//this is just for savety
                      }
                    recurse()
                  }
            };
        };
        marmaInfo.onerror = function(evt) {
            console.log("error");
            return; 
        }
};
//}
recurse()

字符串

相关问题