I want to create an array that holds all of my objects to be able to JSON.stringify()
and pass to my frontend. However, the array that is outside the scope of .then()
is not being updated when I invoke array.push({ obj })
, which screams scoping issue.
How can I update my array? Also, is there a better way to handle my promise objects? The reason behind it is I needed some data from my initial promise that would be passed to my secondary promise. However, I have a feeling that it can be written in a better way.
Thank you for your time.
edit: A word
app.get('/route-submission', (req,res) => {
const station = req.query.station;
const destination = req.query.destination;
var routesAndEvents = [];
/**
* Goal is to pass departures, fares, and events in one JSON object
*/
getStationInfo(station, destination).then( apiResponse => {
const { routes, fare, longLat } = apiResponse;
let departures = manageRoutes( routes, destination );
let fares = manageFares( fare );
let longLatData = manageLongLat ( longLat );
routesAndEvents.push({
departures: departures,
fare: fares
})
getEventInfo( longLatData ).then( apiResponse => {
let eventData = apiResponse.data.events ;
let managedEvents = manageEvents( eventData );
console.log( routesAndEvents );
})
})
res.send ( JSON.stringify(routesAndEvents) );
})
})