B4J Question [BANano] How to handle null values from a db using .GetDefault?

Mashiane

Expert
Licensed User
Longtime User
Hi

I am reading values from a db and it seems the saved db value is null. I am using .GetDefault, expecting that if the value is null, it will return the default value.

I was doing a lowercase to the value however its raising an erorr.

B4X:
Dim shidden As String = fld.GetDefault("hidden", "No")

The error is raised here...

B4X:
If shidden.EqualsIgnoreCase("yes") Then
            elOpt.Put("x-display", "hidden")
        End If

B4X:
Uncaught (in promise) TypeError: Cannot read property 'equalsIgnoreCase' of null

This is what appears on my console.log when I check the values..

1618156525017.png


Thanks
 

Mashiane

Expert
Licensed User
Longtime User
Update: I have tried this temporal solution... I am running this after I read the record from the list from the db. This finds if the key is null / isundefined then assigns a value to it.
Also if that key does not exist, it gets created jic.

B4X:
'set default values to a map if they dont exist
Sub MapSetDefaults(orig As Map, defvalues As Map)
    For Each k As String In defvalues.keys
        Dim v As Object = defvalues.Get(k)
        If orig.ContainsKey(k) Then
            Dim o As Object = orig.Get(k)
            If BANano.IsNull(o) Or BANano.IsUndefined(o) Then
                orig.Put(k, v)
            End If
        Else
            orig.Put(k, v)
        End If
    Next
End Sub

Usage:

B4X:
For Each fld As Map In xfields
        'set default values
        BANanoShared.MapSetDefaults(fld, CreateMap("ondb":"No", "hidden":"No", _
        "active":"No", "readonly": "No", "multiple": "No", "required": "No", "disabled": "No", _
        "inset": "No", "returnobject": "No", "useitems": "No", "autofocus": "No", _
        "yesvalue": "Yes", "novalue": "No"))

Then..

B4X:
Dim shidden As String = fld.GetDefault("hidden", "No")
 
Upvote 0
Top