Anders Metnik
Anders Metnik

Reputation: 6237

Checking a directory for a file in phonegap

Hey 2 questions within this post, they are probably simple for you experienced js guys :-)

First; why is "filename" undefined inside the readEntries when I pass it along?

Second; Why is it always true, when the directory is empty?

Heres my code: I'm calling getPicturepath with a string like "women.png".

function getPicturePath(filename){
    alert(filename); //is correct
    var reader = DATADIR.createReader();
    reader.readEntries(function(entries, filename){
    alert(filename);//is undefined ???
        var doWeHaveIt = function(entries,filename){
            checkForFile(entries,filename)
            };
        if(doWeHaveIt){
            alert('allready have: '+DATADIR.fullPath+filename);

        } else {
            alert('need to download file: '+filename);
        }
    },onError);
}

function checkForFile(entries,filename){
    console.log("The dir has "+entries.length+" entries.");
    if(entries.indexOf(filename)!=-1){
        alert(filename+' allready exists');
        return true;
    } else {
        alert(filename+" doesn't exists");
        return false;
    }
}

Upvotes: 0

Views: 421

Answers (1)

Florian Margaine
Florian Margaine

Reputation: 60767

reader.readEntries(function(entries, filename){

This is the function defining the parameters entries and filename.

For example, this function might do something like:

readEntries: function( callback ) {
    // do something, then
    callback( some, datas );
}

If you just want to use filename in this function, just use it. Like this:

function getPicturePath(filename){
    alert(filename); //is correct
    var reader = DATADIR.createReader();
    reader.readEntries(function(entries){
        alert(filename);// is still correct

The second part (always true) is because of this:

function hi() {}

if ( hi ) {
    // You're always getting there.
}

What I wrote is exactly what you did. I let you guess how to correct that :-)

Upvotes: 1

Related Questions