When attempting a GET request with the Fetch API (Node Fetch) module against a Django REST API, I am encountering a 304 error. I am unsure of how to resolve this issue as it seems to be related to requesting the same data repeatedly. Is there no way around this? How can I store or handle this information differently?
Below is the code for a controller in Express.js:
postsController.index = (req, res, next) => {
try {
let postsResponse = fetch('http://localhost:8000/api/posts.json', {
method: 'GET',
headers: {
'Accept': 'application/json',
"Content-Type": "application/json"
},
})
.then(response => {
return response.json()
})
.catch(err => {
next(res)
})
postsResponse.then((result) => {
res.render('newspapers/index', { title: 'Posts', posts: result })
})
} catch (error) {
next(error)
}
}
Error message:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
express_1 | at ServerResponse.setHeader (_http_outgoing.js:535:11)
express_1 | at ServerResponse.header (/var/www/app/node_modules/express/lib/response.js:767:10)
express_1 | at ServerResponse.contentType (/var/www/app/node_modules/express/lib/response.js:595:15)
express_1 | at ServerResponse.send (/var/www/app/node_modules/express/lib/response.js:145:14)
express_1 | at done (/var/www/app/node_modules/express/lib/response.js:1004:10)
express_1 | at Object.exports.render (/var/www/app/node_modules/jade/lib/jade.js:206:5)
express_1 | at View.exports.renderFile [as engine] (/var/www/app/node_modules/jade/lib/jade.js:233:13)
express_1 | at View.render (/var/www/app/node_modules/express/lib/view.js:135:8)
express_1 | at tryRender (/var/www/app/node_modules/express/lib/application.js:640:10)
express_1 | at Function.render (/var/www/app/node_modules/express/lib/application.js:592:3)
I have tried going to the network tab in Chrome and selecting "disable cache," which resulted in a successful 200 OK status. However, I am still seeing a traceback of the aforementioned problem. Can anyone provide insight into what this means?