mercredi 1 juin 2016

Node.js callback function questions

I'm using mocha and the request module for some tests.

Why is this.restresponse = 'test' and never 'test2' using the code below. The mocha test calls the function code below and then on the callback checks this.restresponse. It is always 'test' even though the body is passed back in the callback.

this.restresponse = 'test';
request(this.formatted, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        this.restresponse = body;
        callback(null, body); // pass back the body
    } else {
        this.restresponse = error;
        callback(error, null); // pass back the error
    }
});

Many thanks

Aucun commentaire:

Enregistrer un commentaire