要在渲染中进行比较的 firebase 数据

Posted

技术标签:

【中文标题】要在渲染中进行比较的 firebase 数据【英文标题】:firebase data to comparison in render 【发布时间】:2018-10-28 05:56:58 【问题描述】:

我需要帮助来比较 firebase 数据 (if) 并渲染正确的组件。

任何帮助将不胜感激。

export default class EstoqueScreen extends Component 

constructor(props)
    super(props);
    var id=firebase.auth().currentUser.uid;
    firebase.database().ref().child('users/'+id).once('value').then((snapshot) => 
        var tipoUsuario=snapshot.val().tipo
    );

render()
    console.log(this.tipoUsuario);
    if(this.tipoUsuario==='admin')
        return(
            <View>
                 <EstoqueAdminScreen/>
            </View>
        )
    else if(this.tipoUsuario==='vendedor')
        return(
            <View>
                 <EstoqueVendScreen/>
            </View>
        )
    

【问题讨论】:

【参考方案1】:
  export default class EstoqueScreen extends Component 

    constructor(props)
        super(props);
        state = 
          loading: true,
          tipoUsuario: null,
        
    

    componentDidMount()
      var id=firebase.auth().currentUser.uid;
      firebase.database().ref().child('users/'+id).once('value').then((snapshot) => 
        this.setState( tipoUsuario: snapshot.val().tipo, loading: false )        
      );
    

    render()
        console.log(this.state.tipoUsuario);
        if(this.state.loading)
          return (
            <View>Loading</View>
          )
        
        if(this.state.tipoUsuario==='admin')
            return(
                <View>
                    <EstoqueAdminScreen/>
                </View>
            )
        else if(this.state.tipoUsuario==='vendedor')
            return(
                <View>
                    <EstoqueVendScreen/>
                </View>
            )
        
        return (
          <View>No hay tipo</View>
        )
    
  

【讨论】:

TypeError: null 不是对象(正在评估 'this.state.tipoUsuario')

以上是关于要在渲染中进行比较的 firebase 数据的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Firebase 身份验证服务并在我们自己的数据库中拥有用户?

iOS 使用fir 蒲公英 进行内部测试

Xcode & Firebase - 我想要一个应用程序功能,其中菜单选项卡选择要在控制器中获取的数据

Firebase 数据比较 swift

从 Firebase 渲染 FlatList 中的数据

如何使用命令行部署 Firebase 数据库安全规则?