No One
No One

Reputation: 683

Axios: How to get data within axios

I created a search for a unique barcode. Therefore the result will be 0 or 1 because it is unique. If barcode is found, I need to get the ID of that record. How do we do this?

                axios.get("api/findpatronbarcode?q=" + query)
                    .then(({data}) => {
                        this.loanpatrons = data.data;

                        //COUNT RECORDS
                        this.countPatrons = this.loanpatrons.length;
                        console.log(this.countPatrons);


                        //THE PROBLEM IS THE CODE BELOW. IT RETURNS "Undefined"
                        // Get the ID of the record
                        var getID = this.loanpatrons.id;
                        console.log(getID)
                    });

Upvotes: 0

Views: 31

Answers (1)

IVO GELOV
IVO GELOV

Reputation: 14269

You can try like this:

axios.get("api/findpatronbarcode?q=" + query)
                    .then(({data}) => {
                        this.loanpatrons = data.data;

                        //COUNT RECORDS
                        this.countPatrons = this.loanpatrons.length;
                        console.log(this.countPatrons);


                        // KEEP IN MIND THAT "loanpatrons" is Array
                        // so first get the first member of the Array
                        // and only then Get the ID of the record
                        var getID = (this.loanpatrons[0] || {}).id || '';
                        console.log(getID)
                    });

Upvotes: 1

Related Questions