我正在尝试使用一个调用apicalls并使用promise的apiclient但是我得到了错误:
"Cannot read property of 'then' of undefined"
我希望apiCalls只能"返回"我从服务器修改或获取的数据,以及能够在我的代码中调用的ApiClient:
const apiClient = new ApiClient()
apiClient.getCharacterOrders(character.character_id, character.access_token)
然后我会在处理时通过事件发送它。但ApiClient永远不会从ApiCalls获得数据"返回"。
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
答案 0 :(得分:1)
通过在HTTP.get前添加“return”并删除“this.response”来解决它,因为它返回了response.data.data