I'm working on fetching nested data objects. Although I can successfully retrieve the object data in the console, I encounter an issue when attempting to access this data using return res.json(imageObject)
. Instead of retrieving all key-value pairs of the object, only the first pair is returned. The error message displayed in the console reads:
UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
.
The console data looks like this: { bigImage: 'https://url.com' }
router.get("/", async(req, res) => {
//...fetching data
if (mediaData.type === "type1") {
let allData = await mediaData.media;
allData.map(async(Data) => {
if (Data.imageType === "big") {
let bigImage = await Data.url;
let imageObject = {
bigImage
};
console.log(imageObject);
return res.json(imageObject);
}
});
}
});