Ax ios: Как получить данные в Axios - PullRequest
0 голосов
/ 29 апреля 2020

Я создал поиск уникального штрих-кода. Поэтому результат будет 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);


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

1 Ответ

1 голос
/ 29 апреля 2020

Вы можете попробовать вот так:

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