【问题标题】:How to check if is null in react native?如何检查本机反应是否为空?
【发布时间】:2021-02-20 06:04:51
【问题描述】:

我正在尝试验证来自 firebase 的数据。

试试{

 const id = this.context.getCurrentUser( { } ).uid  //get user's id

 const user = await this.context.validPlantao(id) // check if got data from firebase. 

在 'const user' 之上它正在工作,如果有一些数据,则返回这些信息,但如果为空则返回 null。

我必须验证下面的代码

            if(user == null){
                console.log('can register data') 
                                
            }else{
                console.log('you even are registered') 
            }

If 条件不起作用。 当 firebase 'const user' 中没有任何内容时返回 null。我想要做的是检查,如果没有注册,他们可以保存。但是,如果有注册,请显示警报消息

【问题讨论】:

  • 我更喜欢安装 Lodash 并检查 isNull 或 isEmpty。您还可以通过开发使用其他简单的方法

标签: javascript reactjs firebase react-native native


【解决方案1】:

试试

        if(user){
            console.log('can register data') 
                            
        }else{
            console.log('you even are registered') 
        }

【讨论】:

    【解决方案2】:

    试试这个:

    If (!user) {
    DO stuff
    } else {
    Do something else
    }
    

    如果那个 dus 不起作用,请尝试记录可能的解决方案,直到你得到它在 null 时给出 true。 喜欢:

    console.log(user === false);
    console.log(user === null) ;
    

    如果一个值为 null,如果你用 false 进行检查,它也应该返回 true

    【讨论】:

      【解决方案3】:

      你可以检查

      if(variable){Do Something} else {Do Something}
      

      或者,如果您想检查对象中的键并使用 babel,您可以使用 chaning 之类的方法

      if(obj?.key){Do Something} else {Do Something}
      

      如果你有兴趣返回一些简单的东西,你可以使用速记检查

      return variable ? "Do something if True" : "Do something if False"
      

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 2020-09-20
        • 1970-01-01
        • 2015-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多