2017-10-16 62 views
0

這裏是我的代碼發送:我使用的是貓鼬查詢結果取和響應

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" }); 
      }) 
     } 

    }); 

,當我運行此代碼 它表明

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) 
+0

什麼是文件/行:E:\ learnNode \ insta \ routes \ api.js:830:25? – dpetrini

+0

res.json({response:1,data:result,message:「IMage List」}) – pushpendra

回答

0

確定。根據回答here,您可能會發送多個回覆。檢查你的代碼。 獨特可能有多個元素,因爲您正在使用arr.filter。如果是這樣,Image.find可能會不止一次地在此路由中發送多個響應。你可以檢查一下,把一些console.log的內部循環和回調計算在內。