Error returning values using $.getJSON

Asked

Viewed 46 times

1

I’m trying to make a page about data from covid-19! I’m trying to get the total number of dead from two different days using the following code:

var ontem = $.getJSON('https://covid19-brazil-api.now.sh/api/report/v1/brazil/2020' + mes.val() + '0' + diaAnt(), {"deaths": ""}).success(function(data) {
    var totais = data.data.reduce((a, b) => a + b.deaths, 0);
    return totais;
  })

var hoje = $.getJSON('https://covid19-brazil-api.now.sh/api/report/v1/brazil/2020' + mes.val() + dia.val(), function(data) {
      var totais = data.data.reduce((a, b) => a + b.deaths, 0)
        
});

The problem is that it always returns me the following data:

Object { readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, … }

I’ve tried everything but I can’t get the exact amount. Can anyone help?

Edit.: What I want to do is subtract the value of the variable "today" with the variable "yesterday"

1 answer

1


ontem and hoje are the objects that make the request, the returned data are available in the functions function(data) { /* faça qualquer coisa aqui */}

To wait for both answers use when

var ontem = $.getJSON('https://covid19-brazil-api.now.sh/api/report/v1/brazil/20200816', {"deaths": ""})
var hoje = $.getJSON('https://covid19-brazil-api.now.sh/api/report/v1/brazil/20200817');
var sub = $.when(ontem, hoje).done(
    function(dadosDeOntem, dadosDeHoje){
        debugger;
        /* faça tudo aqui */
    }
);
/* faça nada aqui */
  • I used the code: var sub = $.when(yesterday, today). done(Function(totals, info){ total Return - info; }) console.log(sub); but still it returns to me Object { state: >, Always: ṃ, then: ?, Promise: ?, pipe: ?, ... }

  • 1

    @Pedromatiasdesouza var sub will not solve your problem, return totais - info does not modify sub, everything needs to be done within the function defined in done.

Browser other questions tagged

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