0

Here is my code:

    hArr = Array();
    hash.find({ hash_name: new RegExp('^' + req.body.hash + '$', "i") }, function(err, doc) {
        //Do your action here..
        if (err) {

            res.json({ response: 0, data: "fail", message: " Image Downloadding" });
        } else {
            for (var h = 0; h < doc.length; h++)
                hArr.push(doc[h].img_name);
            var unique = hArr.filter(function(elem, index, self) {
                return index == self.indexOf(elem);
            })

            Image.find().where('img').in(unique).exec(function(err, result) {
                res.json({ response: 1, data: result, message: "IMage List" });
            })
        }

    });

and when I run this code it show

Listening for request
events.js:160
      throw er; // Unhandled 'error' event
      ^
Error: Can't set headers after they are sent.
    at ServerResponse.setHeader (_http_outgoing.js:359:11)
    at ServerResponse.header (E:\learnNode\insta\node_modules\express\lib\response.js:730:10)
    at ServerResponse.send (E:\learnNode\insta\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (E:\learnNode\insta\node_modules\express\lib\response.js:256:15)
    at E:\learnNode\insta\routes\api.js:830:25
    at E:\learnNode\insta\node_modules\mongoose\lib\query.js:2819:18
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)
mdziekon
  • 3,531
  • 3
  • 22
  • 32

1 Answers1

0

Ok. According to the answer here, you may be sending more than one response. Check your code. unique may have more than one element as you are using arr.filter. If so, Image.find may be called more than once sending more than one response in this route. You may check it putting some console.log's inside loops and callbacks to count.

dpetrini
  • 1,169
  • 1
  • 12
  • 25