在解决包含Firestore嵌套get的Promise时为什么不更新数组的问题我很少感到困惑。
代码如下:
let userArray = [];
return new Promise(resolve => {
channelPaticipationRef
.where('channel', '==', channel.id)
.get()
.then(participationSnapshot => {
participationSnapshot.forEach(participationDoc => {
const participation = participationDoc.data();
participation.id = participationDoc.id;
if (participation.user != userId) {
usersRef
.doc(participation.user)
.get()
.then(user => {
if (user.data()) {
const userData = user.data();
userArray = [
...userArray,
{
userId: userData.id,
userFirstName: userData.firstName,
userLastName: userData.lastName,
userIsOnline: userData.isOnline,
userProfilePictureURL: userData.profilePictureURL,
userLastTimestamp: userData.lastOnlineTimestamp,
},
];
console.log(1, userArray)
}
})
.catch(error => console.log(error));
}
console.log(2, userArray)
});
console.log(3, userArray)
resolve(userArray);
});
});
在控制台中它返回
2 []
2 []
3 []
1 [{…}]
1 (2) [{…}, {…}]
promise返回一个空数组
如果我使用扩展运算符推送或更新数组,为什么不注册?