0

Here is my code

module.exports.comparator = function(req, res){
    Buyer
    .find()
    .exec(function (err, buyer) {
        if (err) {
            console.log("Error finding buyer");
            res
                .status(500)
                .json(err);
        } else {
            console.log("Found buyer", buyer.length);
            res
                .json(buyer);
            buyerData = buyer;
        }
    });

    Seller
        .find()
        .exec(function (err, seller) {
            if (err) {
                console.log("Error finding seller");
                res
                    .status(500)
                    .json(err);
            } else {
                console.log("Found seller", seller.length);
                res
                    .json(seller);
                sellerData = seller;
            }
        });

})

As you can see i am trying to get 2 collections and storing them into a variable because i want to do further coding as...

buyerData.forEach((buyer) => {
    sellerData.forEach((seller) => {
      console.log(buyer.bidPrice , seller.askPrice)
      if(buyer.bidPrice == seller.askPrice){

but whenever i run it i get an error like this

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:491:11)
    at ServerResponse.setHeader (_http_outgoing.js:498:3)
    at ServerResponse.header (E:\eClass\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (E:\eClass\node_modules\express\lib\response.js:170:12)

So is it possible for me to get the data from these different collections and do other computations on them?

shashank
  • 45
  • 6
  • 2
    you are seeing this error because you've already sent response in first query & you sending it again in next. – Rahul Bisht May 14 '18 at 07:13
  • As stated, you cannot send the response "twice". You probably don't really want to return all the data from each collection anyway. Chances are there is "some relation" between the data and as such you likely want a "join" and probably to do some "computation" actually "in the database" instead of pulling all the database into the client. Primarily you don't seem to be understanding that these two "async" functions don't wait for each other to complete before continuing to the next. The marked duplicates should help you with those problems. – Neil Lunn May 14 '18 at 07:59

1 Answers1

1

You can promisify mongoose, and next get full control of your async operations, i.e.:

getBuyer()
.then( buyer => getSeller(buyer) )
.then( calculateAndResponse )
.catch( handleError );

So your logic about getting seller, buyer and manipulate them goes into small methods.

You can also consider using async await, if your env allows to do that. Then codebase could be something like this:

const buyer = await getBuyer();
const seller = await getSeller(buyer);
const response = await calculate(buyer, seller);
//...

This also can be done without promises via callbacks, but it most likely become a callback hell, so from my view using promise is better.

Alejandro
  • 5,834
  • 1
  • 19
  • 36
  • Mongoose already supports Promises. Which you would know if you actually used it and did not just pick up an answer from 7 years ago. – Neil Lunn May 14 '18 at 07:55
  • well i do understand the point that i could use promises but the parameter 'buyer' and also calculateAndResponse could explain them more specifically? thank you :) – shashank May 14 '18 at 08:07
  • @NeilLunn sorry, I probably missed the moment when mongoose promises becomes a built in. And from question's code example codebase looks a bit legacy, so I just wanted him to note that his question is more about async management then particular response error. – Alejandro May 14 '18 at 08:30
  • @shashank the idea is that `getBuyer` returns promise which is resolved with buyer, and `getSeller` with seller and buyer, and in `calculate` you do a needed management with them. – Alejandro May 14 '18 at 08:40
  • @shashank however if you need manipulate a bunch of seller buyer, then https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb - this is more preferable to you – Alejandro May 14 '18 at 08:43
  • Hey @Alex Cud you recomend some promises and async node tutorial or code reference to learn they always confuse me a lot! :) – shashank May 14 '18 at 10:03
  • Actually, duplicated answers have useful links, and you can have a look there. I.e.: https://developers.google.com/web/fundamentals/primers/promises – Alejandro May 14 '18 at 11:21