Search code examples
javascriptgoogle-chrome-apphtml5-filesystem

Cannot read property 'chrome' of undefined


What is causing it and how to avoid it? I cannot catch the exception.
The first time chooseEntry is cancelled createWriter throws an exception sometimes, I can catch this one:

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

then when I try to call chooseEntry again this exception is thrown:

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

Solution

  • To avoid first exception check the fileEntry for undefined,

           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); }