0

I'm making multiple GET requests by building the URLs dynamically.

For any error, I want to grab the value of response.config.url, process it, and push the resulting value into an object.

The code below works fine when I only get ONE error.

When more than one error is returned, only the value from the last error is pushed into the object. I guess that's because it overwrites the previous one(s).

How do I prevent that? How do I make sure that all values get pushed into the object when there is more than one error?

(Note: annotation is an array of strings I get from an input field; _ is lodash)

function checkVariants(annotation) {
    var lemmas = annotation.text.split(' ');
    var results = [];
    var words = [];
    for (var i = 0; i < lemmas.length; ++i) {
        var urlLemmas = encodeURIComponent(lemmas[i]).toLowerCase();
        results.push(urlLemmas);
        $http({
            method: 'GET',
            url: 'https://xxxxxxx.org/variants/' + results[i] + '.txt'
        }).then(function successCallback(response) {
            console.log('Success: ', response.status)
        }, function errorCallback(response) {
            var url = response.config.url;
            words = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
            _.extend(annotation, {
                variants: words
            });
        })
    }
}
1
  • How are you calling the multiple requests? Inside a loop? What is annotation, and what is the output (with multiple error strings) that you want? Commented Nov 6, 2015 at 22:42

2 Answers 2

2

Make your variants property an array and add your words to it:

$http({
    method: 'GET',
    url: 'https://wwwcoolservice.org/variants/' + results[i] + '.txt'
}).then(function successCallback(response) {
    console.log('Success: ', response.status)
}, function errorCallback(response) {
    var url = response.config.url;
    words = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));                
    if (!annotation.variants || !annotation.variants.length) { // First error: create the array
        annotation.variants = [words];
    } else { // next errors: add to the array
        annotation.variants.push(words);
    }
});

With this solution you need to detect whether you have already the variants property, so there is added value any more in calling _.extend.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't think that's correct. I get annotation.variants.push is not a function.
That means your variants property already exists in the annotation object before the first error occurs. Can you show where you initialise it? In the mean time I have adapted the code to take this into account. If the variants property is not an array, it gets reset to an array with the first error word.
Thanks for your input, but that's not the way it's supposed to work. If, for example, words already has three values and I add another one, your current code will add the three old values as well as the new to the existing three! That totals 7 values instead of the correct 4.
OK, I couldn't know that you were repeating this code and were not reinitialising your apparently global variables, nor that you did not want to accumulate the error logging. That context was not given in the question. Anyway good that you have solved your problem.
Totally fair enough, your answer put me on the right track, so +1 :)
0

I managed to get what I wanted.

Here's the code just for reference:

function checkVariants(annotation) {
        var lemmas = annotation.text.split(' ');
        var results = [];
        var oedVars = [];
        for (var i = 0; i < lemmas.length; ++i) {
            var urlLemmas = encodeURIComponent(lemmas[i]).toLowerCase();
            results.push(urlLemmas);
            $http({
                method: 'GET',
                url: 'https://XXXXXX.org/variants/' + results[i] + '.txt'
            }).then(function successCallback(response) {
                console.log('Success: ', response.status)
            }, function errorCallback(response) {
                var url = response.config.url;
                var words = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
                oedVars.push(words);
                _.extend(annotation, {
                    variants: oedVars
                });
            })
        }
    }

Comments

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.