0

I am currently writing an API to unzip files to the web browser sandbox filesystem. I have a basic need to pass a parameter to a function that in turn is itself passed as a parameter. Here is some code to illustrate the issue:

//Request a permanent filesystem quota to unzip the catalog.
function requestFilesystem(size){
    window.webkitStorageInfo.requestQuota(PERSISTENT, size*1024*1024, function(grantedBytes) {
        window.requestFileSystem(PERSISTENT, grantedBytes, function(fs) {
            filesystem = fs;
            removeRecursively(filesystem.root, unzip(url), onerror);
        }, onerror);
    }, function(e) {
        console.log('Error', e);
    });
}

//unzip method can be changed, API remains the same.
//URL of zip file
//callback oncomplete
function unzip(URL) {
    importZipToFilesystem(URL, function(){
        console.log("Importing Zip - Complete!");
    });
}

//remove removeRecursively a folder from the FS
function removeRecursively(entry, onend, onerror) {
    var rootReader = entry.createReader();
    console.log("Remove Recursive"+entry.fullPath);
    rootReader.readEntries(function(entries) {
        var i = 0;

        function next() {
            i++;
            removeNextEntry();
        }

        function removeNextEntry() {
            var entry = entries[i];
            if (entry) {
                if (entry.isDirectory)
                    removeRecursively(entry, next, onerror);
                if (entry.isFile)
                    entry.remove(next, onerror);
            } else
                onend();
**Uncaught TypeError: undefined is not a function**
        }

        removeNextEntry();
    }, onerror);
}

If I try to use

function removeRecursively(entry, onend(URL), onerror) { 

there is an error, ao my issue is how to pass around the URL value for the unzip function, this unzip function is used as a callback function on the onsuccess of removeRecursively

2
  • 1
    just pass unzip, don't call it directly. removeRecursively(filesystem.root, unzip, onerror); not removeRecursively(filesystem.root, unzip(url), onerror);. Also, writing function removeRecursively(entry, onend(URL), onerror) { makes no sense at all, how could you execute inside a param list? Commented May 27, 2013 at 13:38
  • @DavidMcMullin Indeed it was a dumb thing to do, I was misinterpreting that passing unzip I was passing it as a function, a function can take parameters, so let's put some parameters in this. I can't that is why I asked the question, it wasn't making sense to me. Thanks for the reply! Commented May 27, 2013 at 13:57

2 Answers 2

3

You're passing the result of unzip to removeRecursively, which is undefined.

What you probably want to do is

removeRecursively(filesystem.root, function() { unzip(url); }, onerror);

Here you pass a function as a parameter, this function calls unzip with the parameter you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Duh... I have to pass it inside a function, I was misinterpreting the code that by passing unzip was already passing it as a function. I was mislead because if I pass onerror("Error unzipping") as a parameter it works. //error logging function onerror(message) { console.error(message); } removeRecursively(entry, unzip, onerror("Error Removing")); - Works. Going to test this solution.
1

You are calling

removeRecursively(filesystem.root, unzip(url), onerror);

but unzip doesn't return anything

function unzip(URL) {
    importZipToFilesystem(URL, function(){
        console.log("Importing Zip - Complete!");
    });
}

So the second argument of removeRecursively (onend) becomes undefined, which probably causes the error when you trying to use it as a function.

If you want to use unzip function as a callback, you should just pass unzip (instead of unzip(url)) without calling it and then call onend(URL) inside removeRecursively.

1 Comment

The issue is that i want to pass it different values for the URL, So I want unzip to have as a parameter a URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.