MissKnacki
MissKnacki

Reputation: 279

How to get values of a ajax response

In my Javascript file, I get datas from server with an Ajax call :

this.$http.get("/data?startDate="+this.filtres.startDate+" "+this.filtres.startHour+"&endDate="+this.filtres.endDate+" "+this.filtres.endHour).then(function(response) {
    this.todos = response.body;
    this.$forceUpdate();
}); 

response.body looks like this : enter image description here

(1) [...]
    0: Object { nbPieces: Getter & Setter, TRE: Getter & Setter, TRS: Getter & Setter, ... }
    __ob__: {…}
​        dep: Object { id: 35, subs: [] }​​
        value: Array [ {…} ]
        vmCount: 0​​
    <prototype>: Object { walk: walk(), observeArray: observeArray(), … }
    length: 1

Now I would like to put value of nbPieces, TRE and TRS into variables. I tried by doing response.body.values() but I doesn't work.

Upvotes: 0

Views: 102

Answers (1)

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

Your response.body is an array with an object. If it's always going to have length of 1 then this will work for you:

const [{ nbPieces, TRE, TRS }] = response.body;

and then you can use it like normal variables:

console.log(nbPieces, TRE, TRS);

Upvotes: 1

Related Questions