简体   繁体   中英

Express app.param issue with res.send

I am trying to send a response but I am not quiet sure how to do that. I get this error Can't set headers after they are sent when I try to run "res.send or res.response" this code.

The way am I am creating my routes is that if /id is been entered then I will do a SQL query to check for the existence of that id in TableA. If the id exists then I will get the item eg. food, in the table_type column corresponding to that id. Then I will go to that table_type item named table eg. Food, and I will get the attributes from that table and send a response or render a page.

// parameter middleware that will run before the next routes
app.param('num', function(req, res, next, num) {
  var array = [];
  var id;
  var table;
  client.connect(function(err) {
    if(err) {
      return console.error('could not connect to postgres', err);
    }
    client.query("SELECT id, table_type FROM TableA WHERE id=" + num, function(err, result1) {
      if(err) {
        return console.error('error running query', err);
      }
      else {
        //console.log("###### THIS IS IT!!! #######");
        //console.log(result1);
        id = result1.rows[0].id;
        table = result1.rows[0].table_type.trim();

        var latitude;
        var longitude;
        if (table === "Food") {
          var name;
          var description;
          client.query("SELECT name, description, quantity, color FROM FoodTable WHERE item_id=" + id, function(err, result2) {
            if(err) {
              return console.error('error running query', err);
            }
            else {
              name = result2.rows[0].name.trim();
              description = result2.rows[0].description.trim();
              quantity = result2.rows[0].quantity.trim();
              color = result2.rows[0].color.trim();
              array.push(name, description, quantity, color);
              console.log(array);
              //---- WHY NO WORK? ------
              res.send(array);
            }
            client.end();
          });
        }
      }
      client.end();
    });
  });
  next();
});

Worded Explaination of the code: I am trying to write routes that would check if id exists in TableA using the app.param by calling the SQL query client.query . I cannot use the variables outside the function since the data will not be referenced once the function call is complete. So I have concatenated my query. So once the id exists in TableA I store the id and table_type into a variable and check the type in the if statement. So if the table type was Drinks then I would look in Drinks table to get the attributes associated with the id.

You are calling next() outside the asynchronous database call. So what happens is that next has already been executed and returned a response to the request hence headers have already been set when res.send method is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM