【问题标题】:Iterator getter is not callable迭代器 getter 不可调用
【发布时间】:2019-04-25 05:06:26
【问题描述】:

我正在尝试下载一个 json 文件并将其缓存,但出现错误。 代码如下:

  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        // With the cache opened, load a JSON file containing an array of files to be cached
        return fetch('resource').then(function(response) {
          // Once the contents are loaded, convert the raw text to a JavaScript object
          return response.json();
        }).then(function(files) {
          // Use cache.addAll just as you would a hardcoded array of items
          console.log('[install] Adding files from JSON file: ', files);
          return cache.addAll(files);
        });
      })
      .then(function() {
        // Message to simply show the lifecycle flow
        console.log(
          '[install] All required resources have been cached;',
          'the Service Worker was successfully installed!'
        );

        // Force activation
        return self.skipWaiting();
      })
  );

mysw.js:42 Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Iterator getter is not callable.

cache.addAll(文件);似乎在制造问题。

【问题讨论】:

  • 你作为files传递的类型是什么。根据documentation,它必须是(我引用)“你想要获取并添加到缓存中的字符串URL数组。”

标签: javascript service-worker


【解决方案1】:

然后:

cache.addAll(files);

使用:

files.map((url) => {
    return cache.add(url);
});

因为 cache.addAll(files)fail 当文件的 1 不可访问(HTTP 400,401 等,有时也是 5XX 和 3XX)以避免所有失败当 1 失败时,在 map 循环中使用单独的 catch 语句。

查看此答案了解更多详情:https://stackoverflow.com/a/46865785/6375501

【讨论】:

    猜你喜欢
    • 2019-08-01
    • 2011-03-21
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2011-11-22
    • 2021-08-24
    • 2015-01-13
    • 2020-07-01
    相关资源
    最近更新 更多