Strah Behry
Strah Behry

Reputation: 581

Callback not called Node.Js for a router application

My current code is

resizer.resize(filepath, parsedUrl, fullDestinationPath, function() {
        return self.send(response, 200, {'Content-Type': mime.lookup(fullDestinationPath)}, fs.createReadStream(fullDestinationPath));
    });

The resize runs to

Resizer.prototype.resize = function (filepath, parsedUrl, fullDestinationPath) {
         this.read(filepath, parsedUrl, fullDestinationPath);
};

Which then calls

Resizer.prototype.read = function(filepath, parsedUrl, fullDestinationPath){ 
    something.then(function (somethingElse) {
      // Not relevant for question
   }).catch(function (err) {
       console.error(err);
   });
};

I've had a console.log(1); after this.read in the second code snippet to guarantee that it was completely running through it. But returning to my first snippet it doesn't call my callback. The reason I'm using a callback there is because if I don't the send will be executed before a file is completely saved unless it's a very small file, so self.send needs to be called AFTER .resize which is why I am attempting to use a callback.

I've tried multiple things thinking a syntax error might have be the issue or that it was hanging on something, but I've verified it isn't and it is simply not calling the callback. Did I make some kind of obvious mistake? How do I make it call the callback?

I have read questions/answers like this one How to make a function wait until a callback has been called using node.js and understand how it works and implemented it in the same manner, but it's not working for me and I do not see why. Thanks for taking the time to read through this.

EDIT: After fixing the issue with Roberts answer I've removed the callback and changed the code to this, I am using promises instead of callbacks tho: Snippet 1:

resizer
        .resize(filepath, parsedUrl, fullDestinationPath)
        .then(function() {
            return self.send(response, 200, {'Content-Type': mime.lookup(fullDestinationPath)}, fs.createReadStream(fullDestinationPath));
        });

Snippet 2:

Resizer.prototype.resize = function (filepath, parsedUrl, fullDestinationPath) {
   return this.read(filepath, parsedUrl, fullDestinationPath);
};

Snippet 3:

Resizer.prototype.read = function(filepath, parsedUrl, fullDestinationPath){
    return Jimp.read(filepath)
        .then(function() {
            return //tons of irrelevant code
         })
        .catch(function (err) {
            console.error(err);
        });
};

Upvotes: 3

Views: 332

Answers (1)

robertklep
robertklep

Reputation: 203494

Resizer.prototype.resize doesn't take a callback argument to begin with, so even though you pass one from your main code, there's nothing done with it.

The same goes for this.read(): it has no callback argument either.

Try this:

Resizer.prototype.resize = function (filepath, parsedUrl, fullDestinationPath, callback) {
  this.read(filepath, parsedUrl, fullDestinationPath, callback);
};

And:

Resizer.prototype.read = function(filepath, parsedUrl, fullDestinationPath, callback){ 
  something.then(function (somethingElse) {
    callback(null, somethingElse); 
  }).catch(function (err) {
    console.error(err);
    callback(err);
  });
};

Upvotes: 4

Related Questions