简体   繁体   English

为什么 .filter 方法在我的 Expo 应用程序中没有做任何事情?

[英]Why isn't .filter method doing anything in my Expo app?

I'm building an app with Expo and trying to apply a filter to fetchUsers function which pulls a list of Users from Database that are matched with the Authenticated User.我正在使用 Expo 构建一个应用程序,并尝试将过滤器应用于 fetchUsers 函数,该函数从数据库中提取与经过身份验证的用户匹配的用户列表。 I'm using AWS for backend.我正在使用 AWS 作为后端。 However, when I do fetchedUsers.filter it doesn't return ANY User card.但是,当我执行 fetchedUsers.filter 时,它不会返回任何用户卡。

So I have a function fetchMatches which pulls all the Users that have matched with the Authenticated user like so:所以我有一个函数 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);

}

Then I have a function fetchUsers which takes the matches and filters them down to the matches user ids and compares them to the fetched User ids and excludes them if the userId equals the matched User id so you don't have to swipe on somebody you've already matched with.然后我有一个函数 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
      )
    }));
}

So when I apply this filter to fetchedUsers then NO User cards are returned to Homescreen.因此,当我将此过滤器应用于 fetchedUsers 时,没有用户卡返回到主屏幕。 However, when I remove the filter and just do setUsers(fetchedUsers) it returns the entire array of Users from the Database.但是,当我删除过滤器并仅执行 setUsers(fetchedUsers) 时,它会从数据库中返回整个用户数组。 As of now, there aren't any unhandled promises showing up in console or any type of errors.截至目前,控制台中没有出现任何未处理的承诺或任何类型的错误。 It's just simply not rendering.它只是不渲染。 And I'm using Expo.我正在使用世博会。

Don't use the nested filter() , use some() to perform a test.不要使用嵌套的filter() ,使用some()来执行测试。 And you need to return that result, so don't put {} around it.而且您需要返回该结果,因此不要将{}放在它周围。

setUsers(fetchedUsers.filter(user =>
  matches.some(match =>
    match.matchUser1Id !== user.id && match.matchUser2Id !== user.id
  )
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM