[英]Why isn't .filter method doing anything in my Expo app?
我正在使用 Expo 構建一個應用程序,並嘗試將過濾器應用於 fetchUsers 函數,該函數從數據庫中提取與經過身份驗證的用戶匹配的用戶列表。 我正在使用 AWS 作為后端。 但是,當我執行 fetchedUsers.filter 時,它不會返回任何用戶卡。
所以我有一個函數 fetchMatches ,它會拉出所有與經過身份驗證的用戶匹配的用戶,如下所示:
useEffect(() => {
if (!me) {
return;
}
fetchMatches();
}, [me]);
const fetchMatches = async () => {
const result = await DataStore.query(Match, match =>
match
.isMatch('eq', true)
.or(match => match.matchUser1Id('eq', me.id).matchUser2Id('eq', me.id))
);
setMatches(result);
console.warn(result);
}
然后我有一個函數 fetchUsers 獲取匹配項並將它們過濾到匹配的用戶 id 並將它們與獲取的用戶 id 進行比較,如果 userId 等於匹配的用戶 id 則將它們排除在外,這樣你就不必在某人身上刷卡你'已經匹配了。
useEffect(() => {
if(!isUserLoading && user) {
fetchUsers();
}
}, [me, matches, user]);
const fetchUsers = async () => {
let fetchedUsers = await DataStore.query(User);
setUsers(fetchedUsers.filter(user => {
matches.filter(match =>
match.matchUser1Id !== user.id && match.matchUser2Id !== user.id
)
}));
}
因此,當我將此過濾器應用於 fetchedUsers 時,沒有用戶卡返回到主屏幕。 但是,當我刪除過濾器並僅執行 setUsers(fetchedUsers) 時,它會從數據庫中返回整個用戶數組。 截至目前,控制台中沒有出現任何未處理的承諾或任何類型的錯誤。 它只是不渲染。 我正在使用世博會。
不要使用嵌套的filter()
,使用some()
來執行測試。 而且您需要返回該結果,因此不要將{}
放在它周圍。
setUsers(fetchedUsers.filter(user =>
matches.some(match =>
match.matchUser1Id !== user.id && match.matchUser2Id !== user.id
)
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.