【问题标题】:JSON format between Firebase and React Native FlatlistFirebase 和 React Native Flatlist 之间的 JSON 格式
【发布时间】:2018-08-03 05:14:17
【问题描述】:

这是存储在 Firebase 实时数据库中的数据

"Users" : {
  "Joe" : {
    "name" : "xxx",
    "email" : "xxx"
 },
 "Matt" : {
    "name" : "xxx",
    "email" : "xxx"
 }
}

这就是 React Native Flatlist 中需要的数据

 Users : [
  {
    id : "Joe"
    name : "xxx",
    email : "xxx",
  },
  {
    id : "Matt"
    name : "xxx",
    email : "xxx",
  }
]

componentDidMount()..的某个地方

firebase.database("Users").ref().once('value').then(function(snapshot) {
    var arr = _.values(snapshot.val());
    this.setState({ users: JSON.stringify(arr) });
})

在渲染平面列表中:

   <FlatList
           extraData={this.props}
           data={this.props.users}
           keyExtractor={(item, index) => index.toString()}
           renderItem={({ item }) => (
    ...

我如何使用firebase.database().ref() 并返回平面列表中所需数据的样子?

【问题讨论】:

  • 数据应该是一个数组或对象数组
  • 你能分享一些你尝试过的代码吗?
  • @iRiziya 我只是更新问题
  • 好的,检查一下我的答案

标签: react-native react-native-flatlist react-native-firebase


【解决方案1】:

你可以试试这样的:

首先在我的constructor

constructor(props) {
    super(props);

    this.state = {
        arrData:[]
    };
}

然后从 firebase 获取数据

var ref = firebase.database().ref("Users"); //Here assuming 'Users' as main table of contents   

ref.once('value').then(snapshot => {
    // console.log(snapshot.val());

     // get children as an array
     var items = [];
     snapshot.forEach((child) => {
       items.push({
          id: child.val().id,
          name: child.val().name,
          email: child.val().email,
          phone: child.val().phone,
          status: child.val().status,
          user_type: child.val().user_type

       });
    });

    this.setState({ arrData: items});
});

现在您可以在您的FlatListListView 中填充arrData

希望对你有帮助!

【讨论】:

    【解决方案2】:

    在这里,我得到了用户在对象中发布的问题数组,然后我创建了一个数组,然后将该数组分配给变量。

    componentDidMount(){
      let user =  firebase.auth().currentUser ;
      let uid = user.uid;
      firebase.database().ref('Problems').orderByChild('userid').equalTo(uid)
      .once('value' )
      .then(snapshot => {
         // console.log(snapshot.val());
        var items = [];
         snapshot.forEach((child) => {
           items.push({
              id: child.key,
              problemDescription: child.val().problemDescription,
              problemTitle: child.val().problemTitle,
              problemstatus: child.val().problemstatus,
              timeanddate: child.val().timeanddate,
           });
        });
        console.log(items);
        this.setState({problmes: items}); 
        this.setState({isloading:false});
      })
      .catch(error => console.log(error));
    
    }

    【讨论】:

    • 欢迎来到 Stackoverflow!请编辑您的答案并提供解释。 StackOverflow 不喜欢只有代码的答案,它被放置在一个低质量的帖子队列中以审查删除。为防止删除,请说明它的作用。
    • 见“Explaining entirely code-based answers”。虽然这在技术上可能是正确的,但它并不能解释为什么它可以解决问题或应该是选择的答案。除了帮助解决问题,我们还应该进行教育。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2020-11-29
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多