Axios 承诺链接不起作用
Axios promise chaining not working
我正在尝试使用调用 apicalls 并使用 promise 的 apiclient,但出现错误:
"Cannot read property of 'then' of undefined"
我希望 apiCalls 仅 "return" 我已修改或从服务器获取的数据,并且 ApiClient 能够调用我代码中的某处:
const apiClient = new ApiClient()
apiClient.getCharacterOrders(character.character_id, character.access_token)
然后我会在处理后通过事件发送它。但是 ApiClient 永远不会从 ApiCalls "return".
获取数据
ApiClient file
-----------------------------------------------
import ApiCalls from './ApiCalls'
const apiCalls = new ApiCalls()
class ApiClient {
getCharacterOrders (characterId, accessToken) {
apiCalls.getCharacterOrders(characterId, accessToken)
.then(response => {
this.response = response.data
console.log('Response ' + this.response)
// Send this via an event somewhere
}).catch(e => {
this.errors.push(e)
})
}
}
export default ApiClient
ApiCalls file
-----------------------------------------------
import {HTTP} from './axiosClient'
class ApiCalls {
getCharacterOrders (characterId, accessToken) {
HTTP.get('/characters/' + characterId + '/orders/?token=' + accessToken)
.then(response => {
this.response = response.data
// Do stuff to data here
return this.response
}).catch(e => {
this.errors.push(e)
})
}
}
export default ApiCalls
通过在 HTTP.get 前面添加 "return" 并删除 "this.response" 来解决它,因为它返回 response.data.data
我正在尝试使用调用 apicalls 并使用 promise 的 apiclient,但出现错误:
"Cannot read property of 'then' of undefined"
我希望 apiCalls 仅 "return" 我已修改或从服务器获取的数据,并且 ApiClient 能够调用我代码中的某处:
const apiClient = new ApiClient()
apiClient.getCharacterOrders(character.character_id, character.access_token)
然后我会在处理后通过事件发送它。但是 ApiClient 永远不会从 ApiCalls "return".
获取数据ApiClient file
-----------------------------------------------
import ApiCalls from './ApiCalls'
const apiCalls = new ApiCalls()
class ApiClient {
getCharacterOrders (characterId, accessToken) {
apiCalls.getCharacterOrders(characterId, accessToken)
.then(response => {
this.response = response.data
console.log('Response ' + this.response)
// Send this via an event somewhere
}).catch(e => {
this.errors.push(e)
})
}
}
export default ApiClient
ApiCalls file
-----------------------------------------------
import {HTTP} from './axiosClient'
class ApiCalls {
getCharacterOrders (characterId, accessToken) {
HTTP.get('/characters/' + characterId + '/orders/?token=' + accessToken)
.then(response => {
this.response = response.data
// Do stuff to data here
return this.response
}).catch(e => {
this.errors.push(e)
})
}
}
export default ApiCalls
通过在 HTTP.get 前面添加 "return" 并删除 "this.response" 来解决它,因为它返回 response.data.data