Indefinite value after consulting the bank

Asked

Viewed 47 times

1

I have the following code:

var
    async = require('async'),
    pgPromise = require('../utils/pgPromise');

function anyFunction() {
    var tripItem = [];
    var tripStartEndTripItem = [];
    var tripStartEnd = [1,2,3];
    async.series({
        getIdDriver: function (callback) {
            callback(null, 'ok');
        },

        getFinishedTripsLogisticProvider: function (callback) {
            callback(null, 'ok');
        },

        getTrip: function (callback) {
            callback(null, 'ok');
        },

        getTripItem: function (callback) {
            var query = "select json_build_object ('trip_item', ti.*) from trip_item ti where ti_trip_id = ($1)";

            tripStartEnd.forEach(function(x) {
                pgPromise.db.any(query, [x.json_build_object.trip.tr_id])
                    .then(function(result) {
                       for(var i=0; i<result.length; i++) {
                            tripItem[i] = result[i].json_build_object.trip_item;
                        }

                        trip.trip_item = tripItem;

                        tripStartEndTripItem.push(trip);

                    }).catch(function (error) {
                        callback(error, null);
                    })
            });

            callback(null, tripStartEndTripItem);
       }

    }, function (err, results) {
        if (err) {
            console.log('error', err);
        } else {
            console.log(results);
        }
    });
}

The problem is when I assign the result (which is in the variable result) from the bank’s query to the variable tripItem. When I try to print the result on the screen after iteration of the array tripStartEnd, it results as a value undefined. It probably prints on the console before finishing the query. What I could do for that at the time of the console.log(tripItem);, is the value of the consultation ?

1 answer

1


I think you need to use the async.compose for what you need because the series does not exchange data between functions, they run in series but without sharing data. Of course you can resort to global variables or within the shared scope, but this allows for side effects and makes code less reliable.

After you have the string passing data to the next function you can do so with Promise.All():

function(tripStartEnd, callback) {
    var query = "select json_build_object ('trip_item', ti.*) from trip_item ti where ti_trip_id = ($1)";
    var queries = tripStartEnd.map(x => pgPromise.db.any(query, [x.json_build_object.trip.tr_id]));
    Promise.all(queries).then(arr => {
        const tripStartEndTripItem = arr.map(res => res.json_build_object.trip_item);
        callback(null, tripStartEndTripItem);
    }).catch(callback);
}

this way, within the final callback of the .compose you can receive the final data from the function chain.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.