现在我有麻烦了。当我的用户打开应用程序时
async componentWillMount
像这样:
async componentWillMount(){
const value = await AsyncStorage.getItem("token");
this.setState({ token: value });
var self = this;
await axios.get(server + '/index.php/api/TokenController/isValid', {headers: {Authorization: this.state.token}}).then((res) => {
var resJson = JSON.parse(res.data);
self.setState({userid: resJson.info.id});
});
await axios.get(server + '/index.php/api/FriendsController/getList', {headers: {Authorization: this.state.token}}).then((res) => {
var resJson = JSON.parse(res.data);
this.setState({friends: resJson.friends});
});
this.setState({loading: false});
this.socket.emit('register', {connection_id: this.state.userid});
await this.socket.on('hasMessages', data => Promise.all(data).then(res => {
this.setState({offlineMessages: res});
this.state.offlineMessages.forEach((v) => {
console.log(v) // it stops to first iteration
})
}));
}
(例如:
{
"from_id": "76" ,
"id": "e5f76ad6-761c-4637-a118-321a844a9634" ,
"messages": {
"1546799624050": "Ciao" ,
"1546799630274": "Hola" ,
"1546799633100": "Hello"
} ,
"to_id": "1"
}
好的,这是给我的用户的。所以“hasMessage”的反应是这样的。
现在我必须做两个循环(一个是关于数据,另一个是关于每个数据的消息,我必须为所有消息创建一个消息对象)。我该怎么做?我使用了一个有效的承诺,但是嵌套的forEach在第一次迭代时停止,当我重新加载我的应用程序时,我在控制台中看到了其他迭代……有什么想法吗?
我不太擅长异步/等待/承诺请求,我只是在学习!