【发布时间】:2020-08-22 04:16:19
【问题描述】:
我正在使用 Firebase 创建一个云函数来搜索“等候室”,因为我使用“limitToLast”来仅获取“房间”节点上的最后一个子节点,因此我必须遍历快照子节点才能获取我想要的房间的数据快照(我不知道是否有更好的方法,但它之前工作得很好),问题是我需要在分配“waitingRoomData”后检查一些东西“ forEach”,但我收到一条消息:“类型‘从不’上不存在属性”。我以前从未使用过打字稿,也不知道如何解决这个问题。
let waitingRoomData;
//Join waiting room.
waitingRoomsSnapshot.forEach(waitingRoom => {
waitingRoomData = waitingRoom;
});
if (waitingRoomData === undefined) {
return undefined;
} else {
if (waitingRoomData.child('hostId').val() == context.auth?.uid) {
//Uses an existing room where the current user is the host.
//Update creation time.
return waitingRoomData.child('creationTime').ref.set(admin.database.ServerValue.TIMESTAMP).then(() => {
return waitingRoomData.val();
}).catch(error => {
console.log(error);
return error;
});
} else {
return waitingRoomData.val();
}
}
【问题讨论】:
-
你期望 forEach 循环在完成后完成什么?你真的需要一个 forEach 吗?
-
我有通往 waitingRoomsSnapshot 的路径,我必须让最后一个孩子进入这个节点,女巫我是这样得到的:waitingRoomsReference.limitToLast(1).once('value')。 then(waitingRoomsSnapshot => { ... }) waitingRoomsSnapshot 是父母与孩子的快照(在这种情况下只有最后一个),因为我不知道孩子的钥匙,所以我不能用 .child('' ),所以我发现使用 forEach 我可以访问 waitingRoomsSnapshot 中的孩子
标签: typescript firebase firebase-realtime-database google-cloud-functions