0

I went from passing single values into my node API to passing multiple values and the API stopped sending a response back.

Example of single values for fields:

tracking: "123", // Only one tracking number
carrier: "usps" // Only one carrier code

Example of multiple values for fields:

tracking: [ '9361289691090998780363', '784644233417' ],
carrier: [ 'usps', 'fedex' ]

I think I need to add some sort of loop but I cant figure out how to do this nor do I know what the proper terminology is to look this up. Is this related to aync? I'm lost.

request(options, function (error, response, body){});

Here is my code. Any help / additional information would be grateful in understanding what I am doing wrong.

html.js

// These are sent over in an AJAX call //
var trackingNumber = ['9361289691090998780363', '784644233417'];
var carrierCode = ['usps', 'fedex'];

controller.js

app.get("/api/tracking/retrieve", (req, res) => {

    var carrier = req.query.carrier;
    var tracking = req.query.tracking;

    console.log('carrier array', carrier);
    console.log('tracking array', tracking);

    var options = {
        method: "GET",
        url: 'https://api.example.com/v1/tracking',
        qs: { carrier_code: carrier, tracking_number: tracking },
        headers:
            {
                'api-key': process.env.SECRET_KEY_SE,
                'accept': 'application/json'
            }
    }

    console.log("Url: ", req.url);
    console.log("Query: ", req.query);
    // res.send("ok");

    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('BODY', body)

            var trackingData = JSON.parse(body)
            console.log('trackingData: ', trackingData);
            table = 'tracking_table';
            col = [
                'user_id',
                'tracking_number',
                'carrier_code',
            ];

            val = [
                user_id,
                trackingData.tracking_number,
                options.qs.carrier_code,
            ];

            main.create(table, col, val, function (data) {
                res.json({
                    id: data.insertId,
                    user_id: user_id,
                    tracking_number: data.tracking_number,
                    carrier_code: data.carrier_code,
                });
            })
        }
    })

})
6
  • Please provide more details around the API. If your API only supports a single carrier_code or tracking_number, you might need to do a queue for each combination. Commented Mar 15, 2019 at 4:13
  • I am going to contact the company and ask them. Commented Mar 16, 2019 at 0:30
  • It looks to me like you are sending a response back before making an API call, that could be an issue. Commented Mar 16, 2019 at 0:33
  • @TravisDelly I updated the code to comment out //res.send('ok);. Is this what you were referencing? Commented Mar 16, 2019 at 1:09
  • Ye, so if you need to loop network calls or sync functoins you'll want to use promise.all, you'll want to use a map which will return promises into an array and then call promise.all(arrayofpromises), and within the .then you'll send your json response, Here is more information about promise.all developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Commented Mar 17, 2019 at 2:03

1 Answer 1

1

From your controller I can actually see that as soon as you get the request, you are sending the response back. How can you send response again?

I have commented your res.send('ok') and it works.

app.get("/api/tracking/retrieve", (req, res) => {
    var carrier = req.query.carrier;
    var tracking = req.query.tracking;
    console.log('carrier array', carrier);
    console.log('tracking array', tracking);
    var options = {
        method: "GET",
        url: 'https://api.example.com/v1/tracking',
        qs: { carrier_code: carrier, tracking_number: tracking },
        headers:
            {
                'api-key': process.env.SECRET_KEY_SE,
                'accept': 'application/json'
            }
    }
    console.log("Url: ", req.url);
    console.log("Query: ", req.query);
    // res.send("ok"); <--
    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log('BODY', body)
            var trackingData = JSON.parse(body)
            console.log('trackingData: ', trackingData);
            table = 'tracking_table';
            col = [
                'user_id',
                'tracking_number',
                'carrier_code',
            ];
            val = [
                user_id,
                trackingData.tracking_number,
                options.qs.carrier_code,
            ];
            main.create(table, col, val, function (data) {
                res.json({
                    id: data.insertId,
                    user_id: user_id,
                    tracking_number: data.tracking_number,
                    carrier_code: data.carrier_code,
                });
            })
        }
    })
});
Sign up to request clarification or add additional context in comments.

1 Comment

this fix eliminated a minor error I had on the front end, but it did not provide the response I am looking for from the API I am calling (ie: api.example.com/v1/tracking)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.