json - Can't set headers after they are sent. Nodejs/express with axios -
i have seen error , more wanting know how work around it. need able output data json object. there anyway work around this.
i copying , pasting tutorial , not sure why im getting here except deep request calls maybe?
here error:
error: can't set headers after sent. [0] @ serverresponse.outgoingmessage.setheader (_http_outgoing.js:344:11) [0] @ serverresponse.header (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\express\lib\response.js:730:10) [0] @ serverresponse.send (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\express\lib\response.js:170:12) [0] @ serverresponse.json (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\express\lib\response.js:256:15) [0] @ c:/users/amazo/thrifa/moovr/src/server/api/routes/offers.js:28:21 [0] @ promise.<anonymous> (c:/users/amazo/thrifa/moovr/src/server/api/controllers/store/offercontroller.js:25:7) [0] @ promise.<anonymous> (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8) [0] @ emitone (events.js:77:13) [0] @ promise.emit (events.js:169:7) [0] @ promise.emit (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38) [0] @ promise.fulfill (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mpromise\lib\promise.js:97:20) [0] @ c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\query.js:1406:13 [0] @ model.document.init (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\document.js:254:11) [0] @ completeone (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\query.js:1404:10) [0] @ immediate.cb (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\lib\query.js:1158:11) [0] @ immediate._onimmediate (c:\users\amazo\thrifa\moovr\node_modules\keystone\node_modules\mongoose\node_modules\mquery\lib\utils.js:137:16) [0] @ processimmediate [as _immediatecallback] (timers.js:383:17)
here code:
import offercontroller '../controllers/store/offercontroller'; import requestmanager '../utils/requestmanager'; import axios 'axios'; const offerroutes = (app) => { app.get('/api/offers', (req, res, next) => { axios .get( 'https://d2fzm6xoa70bg8.cloudfront.net/login?auth=e4031de36f45af2172fa8d0f054efcdd8d4dfd62', ) .then((response) => { axios .get('https://d2fzm6xoa70bg8.cloudfront.net/offerfilter?', { headers: { token: response.data.token }, }) .then((response) => { const { data } = response; data.map((item, i) => { offercontroller.findone({ offer: item.offer }, (err, result) => { if (err) { res.json({ message: err, }); return; } res.json({ message: 'offer exist', result, }); }); const params = { offer: item.offer, cert: item.cert, txid: item.txid, expires_in: item.expires_in, expires_on: item.expires_on, expired: item.expired, height: item.height, time: item.time, category: item.category, title: item.title, quantity: item.quantity, currency: item.currency, sysprice: item.sysprice, price: item.price, ismine: item.ismine, commission: item.commission, offerlink: item.offerlink, offerlink_guid: item.offerlink_guid, offerlink_seller: item.offerlink_seller, private: item.private, safesearch: item.safesearch, safetylevel: item.safetylevel, paymentoptions: item.paymentoptions, paymentoptions_display: item.paymentoptions_display, alias_peg: item.alias_peg, description: item.description, alias: item.description, address: item.address, alias_rating: item.alias_rating, alias_rating_count: item.alias_rating_count, alias_rating_display: item.alias_rating_display, geolocation: item.geolocation, offers_sold: item.offers_sold, }; offercontroller.create(params, (err, results) => { if (err) { console.log(err); return; } res.write(results); }); }); }) .catch((err) => { console.log('err 2nd request', err); res.json({ confirmation: 'fail after 2nd request', message: err, }); }); }) .catch((err) => { console.log('err 1st request'); res.json({ confirmation: 'fail before 2nd request', message: err, }); }); }); }; export default offerroutes;
you have res.json(...)
inside call map
means make multiple calls res.json
resulting in setting content-type
header multiple times same res
instance.
Comments
Post a Comment