我正在尝试使用axios构建一个反应应用程序,并且它没有按我预期的那样工作...我猜它是异步问题,我的返回没有等待调用结束... < / p>
我的文件结构可能看起来有些奇怪,但是我需要那样做(不允许更改...)...
我的组件:(它将始终控制台日志“未设置”
links <-
links %>%
group_by(source, target) %>%
summarise(value = n() / nrow(df) * 100)
links
# # A tibble: 11 x 3
# # Groups: source [?]
# source target value
# <chr> <chr> <dbl>
# 1 DBBC_3 DBBC_4 60
# 2 DBBC_3 Not in care_4 40
# 3 DBBC_4 DBC_5 20
# 4 DBBC_4 Not in care_5 40
# 5 DBC_2 DBBC_3 100
# 6 DBC_5 DBC_6 20
# 7 DBC_6 Not in care_7 20
# 8 Not in care_4 Not in care_5 40
# 9 Not in care_5 Not in care_6 80
# 10 Not in care_6 Not in care_7 80
# 11 Not in care_7 Not in care_8 100
nodes <- data.frame(name = unique(c(links$source, links$target)))
links$source <- match(links$source, nodes$name) - 1
links$target <- match(links$target, nodes$name) - 1
nodes$name <- sub('_[0-9]+$', '', nodes$name)
library(networkD3)
library(htmlwidgets)
sankeyNetwork(Links = links, Nodes = nodes, Source = 'source',
Target = 'target', Value = 'value', NodeID = 'name',
units = "%")
我的HomeController:
import * as api from '../../services/home/HomeController';
.......
// Some other stuff in this section
.......
componentDidMount() {
var result = api.getAll();
this.setState({
data1: result
});
}
..........
render() {
if(this.state.data1 === undefined) {
console.log("not set");
}
else {
console.log(this.state.data1);
}
}
axios函数: 从“ axios”导入axios;
import { fetchData } from '../axios/axios';
export const getAll = () => {
return fetchData('https://myAPIUrl'); //removed the correct URL
};
因此,如您所见,结果被记录在axios组件的控制台中,因为此函数具有承诺并等待调用完成。但是我想HomeController的返回值不能做到这一点,对吗?
对于JS来说还很新,有点使整个ASYNC事情混淆了……什么是解决我的问题的最佳解决方案?
谢谢您的帮助! :)