source

배열이 있는 응답에서 Axios 응답 데이터 가져오기

bestscript 2022. 11. 22. 09:32

배열이 있는 응답에서 Axios 응답 데이터 가져오기

서버로부터의 응답입니다.

{"message":"Resource Created","payload": 
[{"id":"5d80270413452b6732fd5217","name":"asdf"}]}

설정하려고 합니다.id내 주(州)에.

지금 시도 중입니다.

axios.post(`http://localhost:8081/companies`, company)
     .then(request => request.data.payload.id)
     .then(id => {
         commit('setCreatedCompanyID', id)
     })

이 라인에 오류가 있는 것 같습니다.

.then(request => request.data.payload.id)

응답 배열에서 첫 번째 ID를 가져오려면 어떻게 해야 합니까?

사용하다myArray[index]에 접속하다index의 -번째 요소myArray어레이:

.then(request => request.data.payload[0].id)

데모:

axios.get(`https://api.myjson.com/bins/rin05`)
  .then(request => request.data.payload[0].id)
  .then(console.log)
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
Check the console

언급URL : https://stackoverflow.com/questions/57965969/getting-axios-response-data-from-a-response-with-an-array