Wednesday, 26 April 2017

nodejs express function not returning the variable from fs.readFile

I have the following function in my nodejs, using express.

function metaInfo (id){

    var dir = 'files/'+id;
    var count = 0

    fs.readFile(__dirname +'/' + dir+'/myfile.json', 'utf8', function (err, data) {

        if (err) throw err;
        obj = JSON.parse(data);
        var myArr = obj.nodes;
        var count = Object.keys(myArr).length;
        console.log("counting :", count)
    });

    return count
};

when I call this function, the count is zero, however, it is the right value inside the fs.readFile . how can I return the updated value of count?



via passion

No comments:

Post a Comment