Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 将数据推送到react native中的数组_Firebase_React Native_Firebase Realtime Database_Ecmascript 6 - Fatal编程技术网

Firebase 将数据推送到react native中的数组

Firebase 将数据推送到react native中的数组,firebase,react-native,firebase-realtime-database,ecmascript-6,Firebase,React Native,Firebase Realtime Database,Ecmascript 6,我一直在尝试将数据从firebase数据库推送到阵列,尽管登录时显示数据未显示 下面是日志 [15:36:08] Get Ref https://xxxx.firebaseio.com/merchants [15:36:08] Gladiator Fitness [15:36:08] Array undefined 这就是我试图执行的代码 componentWillMount() { let initialLoad = true; this.setState({loading:

我一直在尝试将数据从firebase数据库推送到阵列,尽管登录时显示数据未显示

下面是日志

[15:36:08] Get Ref https://xxxx.firebaseio.com/merchants
[15:36:08] Gladiator Fitness
[15:36:08] Array undefined
这就是我试图执行的代码

componentWillMount() {
    let initialLoad = true;
    this.setState({loading: true});

    var merchants = [];

    merchantsRef.once('value').then((snapshot) => { // here too!
      // ... and add it to the matches array
      console.log(snapshot.val().business_name)
      merchants.push({
        business_name: snapshot.val().business_name,
        _key: snapshot.key
      })
      console.log("Array " + merchants.business_name)
    }).catch((error) => { // If you are planning to use this here
      console.log('Error fetching merchant data:', error)
    })

    this.setState({
      data: merchants,
      loading: false
    })

    if (initialLoad) {
      this.setState({loading: false});
      initialLoad = false;
    }

  }

setState
的调用必须在
value
回调中:

var merchants = [];

merchantsRef.once('value').then((snapshot) => { // here too!
  // ... and add it to the matches array
  console.log(snapshot.val().business_name)
  merchants.push({
    business_name: snapshot.val().business_name,
    _key: snapshot.key
  })
  console.log("Array " + merchants.business_name)
  this.setState({
    data: merchants,
    loading: false
  })
}).catch((error) => { // If you are planning to use this here
  console.log('Error fetching merchant data:', error)
})
这是因为回调是异步调用的,这意味着在您的代码中,在填充数组之前,将调用带有
merchants
数组的
setState


查看流量的最简单方法是使用几个位置良好的测井语句:

console.log("Before starting to load");
merchantsRef.once('value').then((snapshot) => { 
  console.log("Data loaded");
})
console.log("After starting to load");
运行此代码时,输出为:

在开始加载之前

开始加载后

数据加载


这可能不是您所期望的,但很好地解释了当您在代码中使用
商家
数组调用
setState
时,为什么数组是空的。

您的数据已经在数组中,请检查
控制台.log(“数组”+商家.business\u name)
,显然,它给出了
未定义的
导致访问无效元素的原因。
在控制台中尝试access first元素,如

console.log("Array " + merchants[0].business_name) or 
console.log("Array " + merchants)

您的console.log应该显示您要检查的阵列的位置:merchants[0]。business.nameperfect现在有意义了。我怀疑,因为这段代码放在数组之后,所以它在执行之前就已经被填充了