React Native和Firebase中的ListView

React Native和Firebase中的ListView

问题描述:

我已经能够使用Firebase身份验证服务登录/登录并使用React Native应用程序在Firebase数据库中添加/删除项目。但问题是我只需显示一项用户已经登录了并非数据库中的所有可用数据。而且我还设法让用户标识已经连接到会话中。这是我的代码:......我想我需要在我的列表视图中使用过滤器,但我不知道这件事React Native和Firebase中的ListView

constructor(props) { 
     super(props); 
     this.tasksRef = this.props.firebaseApp.database().ref("/item"); 
     const dataSource = new ListView.DataSource({ 
      rowHasChanged: (row1, row2) => row1 !== row2, 
     }); 
     this.state = { 
      uid: " ", 
      user:null, 
      loading: true, 
      dataSource: dataSource, 
      newTask: 
     }; 
    } 
    componentDidMount() { 
     this.listenForTasks(this.tasksRef); 
    } 

    listenForTasks(tasksRef) { 
     tasksRef.on('value', (dataSnapshot) => { 
      var tasks = []; 
      dataSnapshot.forEach((child) => { 
       tasks.push({ 
        name: child.val().name, 
        type: child.val().type, 
        id: child.val().id, 
        _key: child.key 
       }); 
      }); 
       this.setState({ 
       dataSource: this.state.dataSource.cloneWithRows(tasks) 
      }); 
     }); 
    } 

    componentWillMount() { 
     const userData = this.props.firebaseApp.auth().currentUser; 
      this.setState({ 
      user: userData, 
      uid: userData.uid, 
      loading: false 
     }); 
    } 
    _addTask() { 
     if (this.state.newTask === "") { 
      return; 
     } 
     if (this.state.newTask && this.state.uid) { 
      this.tasksRef.push({ 
       name: this.state.newTask, 
       type: this.state.newType, 
       id: this.state.uid, 
      }); 
     } 

     this.setState({newTask: ""}); 
     this.setState({newType: ""}); 
     this.setState({uid: ""}); 
     alert("Task added successfully"); 
    } 

    _renderItem(task) { 
     const onTaskCompletion =() => { 
      this.tasksRef.child(task._key).remove() 
     }; 
     return (
      <ListItem task={task} onTaskCompletion={onTaskCompletion} /> 
     ); 
    } 

    render() { 
     return (
      <View style={styles.container}> 
       <StatusBar title="to do List"/>      
       <View style={accountStyles.email_container}> 
        <Text style={accountStyles.email_text}>{this.state.user.email}</Text> 
        <TouchableHighlight onPress={this.logout.bind(this)} style={styles.primaryButton}> 
         <Text style={styles.primaryButtonText}>Logout</Text> 
        </TouchableHighlight> 
       </View> 
       <ListView 
        dataSource={this.state.dataSource} 
        enableEmptySections={true} 
        renderRow={this._renderItem.bind(this)} 
        style={styles.listView}/> 
       <TextInput 
        value={this.state.newTask} 
        style={styles.textEdit} 
        onChangeText={(text) => this.setState({newTask: text})} 
        placeholder="New Task" 
       /> 
       <TextInput 
        value={this.state.newType} 
        style={styles.textEdit} 
        onChangeText={(text1) => this.setState({newType: text1})} 
        placeholder="New Type" 
       /> 
       <FloatingActionButton 
        hideShadow={true} 
        buttonColor="rgba(231,76,60,1)" 
        onPress={this._addTask.bind(this)}/> 
      </View> 
     ); 
    } 

    logout() { 
     this.props.firebaseApp.auth().signOut().then(() => { 
      this.props.navigator.push({ 
       component: Login 
      }); 
     }); 
    } 
} 

https://www.firebase.com/docs/security/guide/index.html您需要设置用户按照特定的规则火力点。这将使您的查询只为特定用户发送相关信息。

当我有同样的问题,我做了什么,这家伙说,在第一个反应FireBase - how to list user-specific data?

“我存储由组ID或用户ID的记载:”

@ user3521011继承人我的代码示例:

static setUserName(userId, userName) { 

    let userNamePath = "/user/" + userId + "/details"; 

    return firebase.database().ref(userNamePath).update({ 
     userName: userName 
    }) 

} 
+0

谢谢你..你可以发布您的代码,请让我可以更好地了解 – user3521011

+0

感谢你的帮助。所以我只是改变了构造这一行: 'this.tasksRef = this.props.firebaseApp .database()。REF(“/用户S/“+ currentUID + ”/项目“);' 并在火力地堡的规则:{ ' ”规则“: \t { ”用户“: { \t ”$ user_ID的“:{” .read” :“data.child('user_id').val()== auth.id”, “.write”:“(data.child('user_id').val()== auth.id)|| (newData.child('user_id').val()== auth.id)“}}}}' 请点击这些链接了解更多详情:https://www.firebase.com/docs/web/guide/retrieving -data.html https://www.firebase.com/docs/security/ – user3521011