Defined function insists its undefined

303 views Asked by At

I have simple little program which defines two functions as variables. These functions both return RSVP Promises so that they can be chained together as I have done below. I use this pattern often but in this case I'm running into madness ...

var opChown = function() { ... };
var opChgrp = function() { ... };

debug('chown: ' + opChown);
debug('chgrp: ' + opChgrp);
// Chain permission operations          
opChown()
    .then(opChgrp())
    .then(function() {
        debug('resolving parent promise');
        resolve();
    })
    .catch(function(err) {
        debug('rejecting parent promise');
        reject(err);
    }
);

While my debug statements clearly show that the two functions are in fact defined, when I execute them I get an unhandled exception:

TypeError: undefined is not a function

Please help me losing my mind. Any and all suggestions welcome.


For some additional context here is the definition of opChown:

var opChown = function() {
    return new RSVP.promise(function(resolve,reject) {
        debug('opChown');
        if(options.ignoreChown) {
            resolve();
        } else {
            var chown = shell('chown',['-R', downgradedUser, destDir]);
            debug('chown command executed; user permission is "%s"', downgradedUser);
            chown.on('exit',function(code) {
                if(code !== 0) {
                    var errMessage = 'Build [' + code + ']:' + ' problems changing ownership on "' + destDir + '" directory to ' + downgradedUser + '.';
                    debug('Problem with chown: %s', code);
                    reject({operation:'chown', message: errMessage});
                } else {
                    console.log(' - %s executed, user permissions changed to %s', chalk.bold('chown'), chalk.bold(downgradedUser));
                    resolve();
                }
            }); // end chown
        }
    });
}; // end opChgOwn          

Now based on @bergi's great pointer that the stacktrace is indeed available to me on Node's 'uncaughtException' event, here is the stacktrace which clearly points to the problem being within the opChown function rather than the function itself:

TypeError: undefined is not a function
    at opChown (/lib/broccoli-watcher.js:117:13)
    at ChildProcess.<anonymous> (/lib/broccoli-watcher.js:167:5)
    at ChildProcess.emit (events.js:98:17)
    at Process.ChildProcess._handle.onexit (child_process.js:809:12)
2

There are 2 answers

1
Bergi On BEST ANSWER

Although I had expected the error message to be something like undefined is not a constructor, it looks as if the line

    return new RSVP.promise(function(resolve,reject) {

should rather be

    return new RSVP.Promise(function(resolve,reject) {
//                  ^

Notice that @SLaks spotted a mistake as well: then takes callback functions. You probably want to use

opChown().then(opChgrp)…
7
SLaks On
.then(opChgrp())

You just called opChgrp immediately and passed its result to then().

Since it doesn't return anything, you get an error.