Axios:如何在axios中获取数据

时间:2020-04-29 05:32:03

标签: axios

我创建了一个唯一条形码的搜索。因此结果将是0或1,因为它是唯一的。如果找到条形码,则需要获取该记录的ID。我们该怎么做?

                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)
                    });

1 个答案:

答案 0 :(得分:1)

您可以尝试这样:

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)
                    });