Search code examples
javascripttizentizen-wearable-sdktizen-web-app

Tizen Web Application Wearable :TypeMismatchError


Getting a TypeMismatchError: Value is not Object in the following codes

var fileHandler = {
    storages:null,
    documentsDIR:null,
    newFile:null,
    dirName:"documents",
    fileName:"test.csv",
    init: function init(){
        //tizen.filesystem.listStorages(this.checkCorruptedRemovableDrives);documents
        tizen.filesystem.resolve(this.dirName, this.resolveSuccessCB,this.resolveErrorCB,"rw");

    },

    resolveSuccessCB: function resolveSuccessCB(result){
        this.documentsDir = result;
        //this.newFile = documentsDir.createFile(this.fileName);
        // Error Here - TypeMismatchError: Value is not Object
        result.listFiles(this.listFilesSuccessCB);
        console.log("All Good in "+this.documentsDir);
    },

    resolveErrorCB: function resolveErrorCB(error){
        console.log("Unable to access "+this.dirName+". Error:"+error.message);
    },

    listFilesSuccessCB: function listFilesSuccessCB(files){ 
        for (var i = 0; i < files.length; i++){
          /* Display the file name and URI */
          console.log("File name is " + files[i].name + ", URI is " + files[i].toURI());
       }
    },



    onStorage: function onStorage(storage){ 
           console.log("Storage found:" + storage.label);
    },
    checkCorruptedRemovableDrives: function checkCorruptedRemovableDrives(storages){
        this.storages = storages;
        for (var i = 0; i < storages.length; i++) 
        { 
            if (storages[i].type != "EXTERNAL"){
                continue;
            }
            if (storages[i].state == "UNMOUNTABLE"){
                console.log("External drive " + storages[i].label + " is corrupted.");
            } else {
                console.log(" Storage = " + storages[i].label );
                // Error here - TypeMismatchError: Value is not Object
                tizen.filesystem.getStorage(storages[i].label, this.onStorage);
            }
        }
    }

};

The error occurs at two places marked as Error Here in the codes.

In tizen.filesystem.getStorage(storages[i].label, this.onStorage); the datatype of the first argument is DOMString and storages[i].label is also of type DOMString. In result.listFiles(this.listFilesSuccessCB); expects a callback fucntion.

Most of the codes have been copied pasted from Tizen FileSystem Guide but still not working.

Please help. Thank in advance!! Also where can I find the "documents" directory, when I browse the filesystem of my wearable device.


Solution

  • Simple Answer change this to fileHandler.

    Do not trust "this" when your function is called in tizen API.

    You seems to use checkCorruptedRemovableDrives to succuess call back of tizen.filesystem.listStorages.

    And you trust "this" in checkCorruptedRemovableDrives is fileHandler.

    But most of Tizen web API callback functions called like following code.

    callback.apply(callback, [].slice.call(arguments, 1));
    

    first argument of apply is passed into callback for this but it is self function object.

    So if you want fileHandler in your function, don't use this use fileHandler direct.

    following code is Tizen web API implementation You can look this following callIfPossible function.

    function listStorages() {
      xwalk.utils.checkPrivilegeAccess(xwalk.utils.privilege.FILESYSTEM_READ);
      var args = validator_.validateArgs(arguments, [
        {name: 'onsuccess', type: types_.FUNCTION},
        {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true}
      ]);
    
      setTimeout(function() {
        var storages = [];
        var cache = commonFS_.getAllStorages();
        for (var i = 0; i < cache.length; ++i) {
          storages.push(new FileSystemStorage(cache[i]));
        }
    
        native_.callIfPossible(args.onsuccess, storages);
      }, 0);
    };
    

    https://review.tizen.org/git/?p=platform/core/api/webapi-plugins.git;a=blob;f=src/filesystem/js/file_system_manager.js;h=622f38d479647584012925773d124d7f9a59e51d;hb=refs/heads/tizen#l158

    NativeManager.prototype.callIfPossible = function(callback) {
      if (!_type.isNullOrUndefined(callback)) {
        callback.apply(callback, [].slice.call(arguments, 1));
      }
    };
    

    https://review.tizen.org/git/?p=platform/core/api/webapi-plugins.git;a=blob;f=src/utils/utils_api.js;h=5502e3f2a46254c7e2da9d838bacd55be9aaab20;hb=refs/heads/tizen#l1141