【问题标题】:React Native Firebase how to fetch data from array of tables with specific userReact Native Firebase 如何从具有特定用户的表数组中获取数据
【发布时间】:2023-03-10 18:10:01
【问题描述】:

我从(特定用户的)导师表中获取“userId”,并根据该 userId 在“tutorCopy”下创建一个表,接受“john@john.com”服务的用户保存其 userId 并用户邮箱。现在当 john@john.com 登录系统时,我想显示分配给他的电子邮件/人员的数量。 这是我到目前为止所做的,但我无法访问 useremail:

 componentWillMount(){
     var refx =firebase.database().ref("tutorCopy")
    refx.orderByChild("table").on("value", function(snapshot) {
        snapshot.forEach(function(data) {
            this.setState({ markers: Object.values(snapshot.val()) })
          //alert(data.useremail)
        });
      });
    }

另外,我只想显示那些从导师表到导师复制的导师 ID 匹配的用户邮件。

【问题讨论】:

    标签: javascript firebase react-native firebase-realtime-database react-native-firebase


    【解决方案1】:

    尝试以下方法:

     componentWillMount(){
         var refx =firebase.database().ref("tutorCopy")
        refx.on("value", function(snapshot) {
            snapshot.forEach((childSnapshot) => {
              childSnapshot.forEach((childSnap) =>{
               console.log(childSnap.val());
              });
            });
          });
        }
    

    您需要迭代两次才能访问useremail,在这种情况下orderByChild("table") 将不起作用,因为您无法访问table 节点,因为您需要更进一步,例如:

    var refx =firebase.database().ref("tutorCopy")
    refx.child(userId).orderByChild("table")
    

    【讨论】:

    • 谢谢先生!这实际上有效,但是当我将标记设置为 childSnap 时,它给了我错误:无法读取属性'setState'
    • 我如何将标记的状态设置为childSnap,它总是说不能设置状态标记为空
    • 我不确定您为什么会收到此错误,您是否在应用程序的其他地方使用了setState
    • 在另一个类中是的,但它在那里工作正常..,这可能是由于 firebase 中的嵌套表,即使我将标记初始化为 state 中的数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2021-04-06
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    相关资源
    最近更新 更多