0

I'm working with nodeJS and expressJS. I Have 2 functions, main functions:

download(imgUrl, imgMD5, function(fileLength, fileSize) {
    console.log(fileLength);

    var qb = data.Img.query();
    qb.where('img_md5', '=', imgMD5).update({
        img_downloaded: 1
    }).then(function() {});
});

and external function

module.exports = function() {
    return function(uri, filename) {
        request(uri, function(err, res, callback) {
            fileLength = res.headers['content-length'];
            var mkdirs = function(fold, callback) {

                var pf = path.dirname(fold);

                fs.exists(pf, function(exists) {
                    var create = function() {

                        fs.mkdir(fold, callback);
                    };
                    if (exists) {
                        create();
                    } else
                        mkdirs(pf, create);
                })
            };
            var folder = ('./downloaded/' + yy + '/' + mm + '/' + dd + '/' + ho + '/');

            mkdirs(folder, function() {
                var r = request(uri).pipe(fs.createWriteStream(folder + filename));
                r.on('close');
            });
            callback(fileLength);
        });
    };
};

but it's fired an error when running:

TypeError: string is not a function

I don't know if I'm uses the callback right or not?

thank you

1
  • @MattBall :line 36, at "callback(fileLength);" Ty! Commented Oct 16, 2014 at 3:11

1 Answer 1

2

Your request() callback parameters aren't quite labelled appropriately. The third argument passed to your callback is the entire buffered (string) body from the response, not a function. So that's why it complains at callback(fileLength);.

Also, because you used a callback (which receives the entire buffered response), there is no need to request the URL again. So you could change this:

mkdirs(folder, function(){
  var r = request(uri).pipe(fs.createWriteStream(folder + filename));
  r.on('close');
});

to this:

mkdirs(folder, function() {
  // `data` here is the third argument to the `request()` callback ...
  fs.writeFile(folder + filename, data);
});

to save an extra HTTP request.

Or if you really do want to stream the response data, you could do:

request(uri).on('response', function(res) {
  // ...

  mkdirs(folder, function() {
    res.pipe(fs.createWriteStream(folder + filename));
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, really want to vote it up but i have an insufficient on rep to do.
I fixed it up! fs.writeFile(folder + filename, data); the data in my function should change to request(uri) and the callback parameter should be in the return functions()

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.