1

I'm having an issue sending the same json object to two separate endpoints. I need to send one copy to a database, and the other copy back to the client. I'm getting can't set headers after they are sent. Which I have gathered is an error saying that res.json() is called once, and can not be called a second time because the headers have been "baked in". I'm pretty newb to development, any suggestions or explanation would be very helpful.

Note: the whole code executes successfully, then crashes the server with the error message above.

paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
        if (error) {
            console.log(error.response);
            throw error;
        } else {
            console.log("Get Payment Response");
            console.log(JSON.stringify(payment));
                         const userData = {paymentID : payment.id};
            UserData.addUserData(userData, function(err, userData) {
              if (err) {
                throw err;
              }
              res.json(userData);
            });
            res.json(userData)
        }
    });
})
Hanley Soilsmith
  • 579
  • 2
  • 9
  • 27

1 Answers1

1

You are right when you write that you can't call res.json() a second time. You can only send one response per request. res.json() sends a response, so you can only call it once (you have probably seen this question already).

You don't have to send a response to the database. Only the client that sent the request should receive a response. Calling res.json() will not send anything to the database. In order to store the userData object in the database, you have to call the function that does that. In your case, I assume you are doing that with UserData.addUserData(). This is where the storing happens, not by sending a response to it.

The function you send in as an argument to UserData.addUserData() is most likely a callback that is called AFTER storing the userData object is finished. Basically, UserData.addUserData() will do all the stuff it's supposed to do, and then continue with the function you have written after that. In there you can call res.json(). You can remove the last call to res.json(). It's not needed as you will call it in the callback function after storing in the database is finished.

paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
        if (error) {
            console.log(error.response);
            throw error;
        } else {
            console.log("Get Payment Response");
            console.log(JSON.stringify(payment));
            const userData = {paymentID : payment.id};
            UserData.addUserData(userData, function(err, userData) {
              if (err) {
                throw err;
              }
              res.json(userData);
            });
        }
    });
})
Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
  • Thanks a bunch. This I'm seeing that I need to use db.open() as a way to establish connection to the database and send data. The tutorial that I followed to create the original code was utilizing a callback. I appreciate you taking the time to answer my question :) – Hanley Soilsmith Jan 12 '19 at 17:01