Can not read property apiRest

2

I have a problem with this api.

this is in html

<div id="summary"></div>

this is in JS

var consuKey = "ck_b04aa6f288ee9a5495dee9c5db0a6b136350e005";
var consuSecr = "cs_1f98d389d0f9b47cd3200023864cf9b7cba50574";

function callurl() {
$.ajax({
   url: 'https://test.juand.org/wc-api/v2/reports/sales?',
   data:{
        filter: {period: "last_week"},
        consumer_key: consuKey,
        consumer_secret: consuSecr
    },
type: "GET",
dataType: "json"
})
.done(function(data){
JsonpCallback(data.reports)
})
.fail(function(data){
console.log("no");
})
}

function JsonpCallback(json) {
for (var i = 0; i < json.length; i++) {
$('#summary').append('<b>Descripción:</b> ' + json[i].total_sales + '<br />');
$('#summary').append('<hr />');
}
}

callurl();

I have the following error

Uncaught TypeError: Cannot read property 'length' of undefined
    at JsonpCallback (VM2284:68)
    at Object.<anonymous> (VM2284:60)
    at fire (VM2283 jquery-2.2.4.js:3187)
    at Object.fireWith [as resolveWith] (VM2283 jquery-2.2.4.js:3317)
    at done (VM2283 jquery-2.2.4.js:8757)
    at XMLHttpRequest.<anonymous> (VM2283 jquery-2.2.4.js:9123)

the idea is that of the result of the json ( total_sales ) but I still do not understand why I get the error, if I call JsonpCallback (data.reports) if I use

JsonpCallback (data.sales)

does not give any results ..

You can help me find a request, Thanks !!

can see my code here

link

    
asked by Juan David 28.03.2017 в 18:36
source

1 answer

2

If you make a console.log of data in the done of your AJAX call, you will see that that object does not contain any array called "reports":

{
  "sales": {
    "total_sales": "157.94",
    "net_sales": "157.94",
    "average_sales": "22.56",
    "total_orders": 2,
    "total_items": 3,
    "total_tax": "0.00",
    "total_shipping": "0.00",
    "total_refunds": 0,
    "total_discount": "0.00",
    "totals_grouped_by": "day",
    "totals": {
      "2017-03-22": {
        "sales": "0.00",
        "orders": 0,
        "items": 0,
        "tax": "0.00",
        "shipping": "0.00",
        "discount": "0.00",
        "customers": 0
      },
      "2017-03-23": {
        "sales": "0.00",
        "orders": 0,
        "items": 0,
        "tax": "0.00",
        "shipping": "0.00",
        "discount": "0.00",
        "customers": 0
      },
      "2017-03-24": {
        "sales": "0.00",
        "orders": 0,
        "items": 0,
        "tax": "0.00",
        "shipping": "0.00",
        "discount": "0.00",
        "customers": 0
      },
      "2017-03-25": {
        "sales": "0.00",
        "orders": 0,
        "items": 0,
        "tax": "0.00",
        "shipping": "0.00",
        "discount": "0.00",
        "customers": 0
      },
      "2017-03-26": {
        "sales": "0.00",
        "orders": 0,
        "items": 0,
        "tax": "0.00",
        "shipping": "0.00",
        "discount": "0.00",
        "customers": 0
      },
      "2017-03-27": {
        "sales": "0.00",
        "orders": 0,
        "items": 0,
        "tax": "0.00",
        "shipping": "0.00",
        "discount": "0.00",
        "customers": 0
      },
      "2017-03-28": {
        "sales": "157.94",
        "orders": 2,
        "items": 3,
        "tax": "0.00",
        "shipping": "0.00",
        "discount": "0.00",
        "customers": 0
      }
    },
    "total_customers": 0
  }
}

That's why, when you pass data.reports to the function JsonpCallback , what you're going through is a undefined that does not have the property length (that's why you get the error you receive). You should check the documentation of the API that you are using to verify that the result you receive is what you expect.

It seems that the closest thing to what you are looking for would be to pass data.sales , but that is not an array but an object, then the% for of the function JsonpCallback will not work either.

As I said, I recommend that you review the API / service documentation that you are using to see what it is going to return to you.

    
answered by 28.03.2017 / 21:59
source