-2

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

})

1 Answers1

0

Since you are invoking the res.send outside the then callback, that line can be invoked before your promise is completed. By that time, the array may not have updated yet. So move your res.send into the second then as follows.

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

And see the comment below by @wrobbinz on how you can avoid nesting promises.

Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17
  • That makes a lot of sense. As for the second question, is there a better way to write the `.then` callback instead of having one inside the other? i.e. pass `longLatData` to `getEventInfo` outside of `getStationInfo`? – hello_there Sep 17 '19 at 17:07
  • You can avoid nesting the promises by chaining them. Return a value from within the `.then()` and the return value will be a promise. Since the return value is a promise, it ALSO has a `.then()` property, that you can use. See here for code example. https://jsfiddle.net/mqz6w87g/ – wrobbinz Sep 17 '19 at 19:37