Javascript Axios承诺链接不起作用
我正在尝试使用调用ApicCalls并使用promise的ApicClient,但我发现错误:Javascript Axios承诺链接不起作用,javascript,axios,Javascript,Axios,我正在尝试使用调用ApicCalls并使用promise的ApicClient,但我发现错误: "Cannot read property of 'then' of undefined" 我希望ApicCalls仅“返回”我已修改或从服务器获得的数据,并且ApicClient能够在我的代码中的某个地方调用: const apiClient = new ApiClient() apiClient.getCharacterOrders(character.character_id, charac
"Cannot read property of 'then' of undefined"
我希望ApicCalls仅“返回”我已修改或从服务器获得的数据,并且ApicClient能够在我的代码中的某个地方调用:
const apiClient = new ApiClient()
apiClient.getCharacterOrders(character.character_id, character.access_token)
然后,当它被处理时,我将通过事件发送它。但是ApicClient从不从ApicCalls“return”获取数据
通过在HTTP.get前面添加“return”并删除“this.response”解决了这个问题,因为它返回response.data.dataProbaste只返回HTTP.get()?其中的内容。/axioClient`?
getCharacterOrders
不返回任何内容。在HTTP.get(…)之前添加return
。默认情况下,没有return
语句的函数返回undefined
(因此出现错误“无法读取undefined的'then'属性”
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