2

I have a simple express application which receives a POST request and performs calls to different API's. I want to send out an email when one or more of the API requests return an error. A simplified version of what I am doing is as follows: File app.js

var express = require('express');
var request = require('request');
var email=require('./elasticemail.js');

var numApiRequests=3;
var wasError=false;
var responseToClient;
app.post("/", function(req, res) {
  responseToClient=res;
  request({/*first request data*/},function (error, response, body){
    if (error)
      wasError=true;
    done()
  }
  request({/*second request data*/},function (error, response, body){
    if (error)
      wasError=true;
    done()
  }
  request({/*third request data*/},function (error, response, body){
    if (error)
      wasError=true;
    done()
  }

}//app.post

function done(){
  isDone++;
  if (isDone===numApiRequest){
    if (wasError){
      email.send('an error occurred')
      responseToClient.send('there was an error')
    }
    else 
      responseToClient.send('everything was fine')
  }
}//done

This works fine as long as there is just one request at a time, but if there is more than one request at a time, the variables "wasError", "responseToClient" are affected by the actions performed for each request. Is there any way so that these variables can be accessed by the done() function but are not accessed by other requests? I know that I could declare such variables inside app.post() and pass them as a parameter to done(), so that they don't need to have global scope, but I am looking for a generic method to limit the scope of a variable to the context of a request.

2 Answers 2

1

You've explicitly put those variables in the global scope. You want to make sure variables are scoped correctly so they only apply to the request your working on.

app.post("/", function(req, res) {
    var wasError=false;
    var responseToClient;
    numApiRequests=3;

    responseToClient=res;
...

And you can add the done function to the app post handler.

app.post("/", function(req, res) {
    ...
    function done() {
        ...
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @jeremy. This seems to be exactly what I was looking for. Thought I have one more question: what if I need to use the done() function somewhere else in my code, for instance in app.post() ? is there any way to declare a funcntion somewhere else and make it use the variables that are local to the place where it is called? ie.: function done(){...} app.post(....){ var wasError=false; var responseToClient; ... done()/*uses app.post's local variables*/ } app.get(....){ var wasError=false; var responseToClient; ... done()/*uses app.get's local variables */ }
There are several ways to do this in javascript, you can use function binding to bind this to the done() function (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…)
0

Instead of using a global (responseToClient), you could pass res directly to done() so that you have done(res) in each request() callback. Then just modify the done function appropriately.

1 Comment

Thanks but this is what I explicitely said I was not looking for

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.