【问题标题】:Cannot read property 'chrome' of undefined无法读取未定义的属性“chrome”
【发布时间】:2013-11-20 05:04:11
【问题描述】:

是什么原因造成的,如何避免?我无法捕捉到异常。
第一次chooseEntry被取消createWriter有时会抛出异常,我可以捕捉到这个:

Error in response to fileSystem.chooseEntry: TypeError: Cannot call method 'createWriter' of undefined

然后当我再次尝试调用 chooseEntry 时,会抛出此异常:

try{
    chrome.fileSystem.chooseEntry({ type:'saveFile',suggestedName:$("#mydiv").text()+".jpg",accepts:[{ extensions:['jpg'] }] },function(fileEntry){
        try{
            fileEntry.createWriter(function(writer) {
                writer.onerror = errorHandler;
                writer.onwriteend = function(e) { console.log('write complete'); };
                writer.write(data); //data is a blob
            });
        }catch(err){ console.log("error",err.message); }
    });
}catch(err){ console.log("error",err.message); }

> Error in response to fileSystem.chooseEntry: TypeError: Cannot read property 'chrome' of undefined

【问题讨论】:

  • 查看callBack函数内部的fileEntry参数,取消后可能为null。

标签: javascript google-chrome-app html5-filesystem


【解决方案1】:

为了避免第一次异常检查未定义的文件条目,

       try{
          if(fileEntry) { 
              fileEntry.createWriter(function(writer) {
                writer.onerror = errorHandler;
                writer.onwriteend = function(e) { console.log('write complete'); };
                writer.write(data); //data is a blob
             });
           }
        }catch(err){ console.log("error",err.message); }

【讨论】:

  • 我认为这解决了这两个错误。非常感谢,我会继续检查的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
  • 2020-10-30
  • 2019-10-09
  • 1970-01-01
相关资源
最近更新 更多