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