简体   繁体   English

如何在本机反应中循环多个数组

[英]how to loop multiple arrays in react native

I am actually want to iterate a collection in firestore which contains multiple documents, and i want to return the doc that contain field uid equal of my uid. 我实际上想在Firestore中迭代一个包含多个文档的集合,并且我想返回包含字段uid与我的uid相等的doc。

that's the data in firestore: 那是firestore中的数据:

const { user } = this.props ;
console.log("getting user data: ", user )

result on console

that's my code: 那是我的代码:

render() {
   const auth = this.props.auth;
   console.log("getting user id: ", auth.uid);

   const userData = user.map((item)=>(
      (item.uid) = (auth.uid)
        ? <Text color="white" size={28} style={{ paddingBottom: 8 }}>
               { item.displayName } </Text>
        : <Text color="white" size={28} style={{ paddingBottom: 8 }}> Error 
          </Text>
      )
    );
return (

              <Block style={styles.profileTexts}>
                  {userData}
              </Block>
   )
}
const mapStateToProps = ( state ) => {
  console.log("state firebase",state);
  return{
    auth: state.firebase.auth,
    user: state.firestore.ordered.users,
  }
}
const mapDispatchToProps = (dispatch) => {
  return {
    signOut: () => dispatch(signOut()),
  }
}

export default compose(
   connect(mapStateToProps, mapDispatchToProps),
   firestoreConnect([
       { collection: 'users'},
   ]))(Profile)

But i got this error: "TypeError: undefined is not an object (evaluating 'o.map')" 但是我遇到了这个错误:“ TypeError:未定义不是对象(正在评估'o.map')”

inside your map you are using assignment operator instead of comparison operator. 在地图内部,您使用赋值运算符而不是比较运算符。

if you just want to return an item which is same as auth.uid use a filter instead 如果您只想返回与auth.uid相同的项目, auth.uid使用过滤器

Create a function which returns the item.uid === auth.uid 创建一个返回item.uid === auth.uid

  userFn = () => {
      const {user} = this.props;
      const authUser = user.filter(item => {
          return item.uid === auth.uid
      })
     if (authUser.length>1){
         return <Text>{authUser[0].displayName}</Text>
     }
     else return <Text>Error</Text>
  }

then inside return statement 然后在内部返回语句

  return (
      <View style={{flex:1,justifyContent:'center'}}>
          {this.userFn()}
      </View>
  );

You use user in render() method, but I don't see where did you get them? 您在render()方法中使用了user ,但我看不到您从哪里得到的? I think this can fix the problem. 我认为这可以解决问题。

render() {
   const auth = this.props.auth;
   console.log("getting user id: ", auth.uid);


   const user = this.props.user // <=== add this line
   const userData = user.map((item)=>(
   ...

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

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