If / Else Statement Parse HELP!

Below I have my code that handles searching for a user in parse.  

The “if” statement runs fine when the text field’s text matches a registered user.  However, when the text field’s text doesn’t match a registered user, the “else if” statement should run.  However, it doesn’t.

My question is:  ​How can I get the else if statement to run when the text field text doesn’t match a registered user?

I am guessing there is some kind of debugging method to set as the conditional statement, for example: elseif (value.results == nil) or something along those lines that can detect when no users are returned/found.  Thanks!

  1. –search bar function to find user
  2.    local function onSearchUser( ok, res, info )
  3.         if (ok == true) then
  4.            
  5.            for key, value in ipairs( res.results ) do
  6.  
  7.  
  8.            if (value.username == searchInput.text) then
  9.                 – user found!
  10.  
  11. print(“USER FOUND!”)
  12.  
  13. print( "Push notification target is: "…searchInput.text  )
  14.  
  15. gamesettings.pushNotificationTarget = “”…searchInput.text
  16. loadsave.saveTable(gamesettings, “settings.json”)
  17.  
  18. elseif (value.username ~= searchInput.text) then
  19.  
  20. native.showAlert( “No User Found”, searchInput.text…" does not exist!",  { “” }  )
  21.  
  22.  
  23. end
  24.       end
  25.    end
  26. end
  27.   
  28.   
  29.   parse.macro.findUserByUsername( ‘’…searchInput.text)
  30.   :response(onSearchUser)

You don’t need an elseif that is the opposite of the if.  Just use ‘else’

-- WRONG if (value.username == searchInput.text) then elseif (value.username ~= searchInput.text) then -- This could trigger false if both are nil. end -- RIGHT if (value.username == searchInput.text) then else end

@roaminggamer

I just tried exactly what you said, and the else statement won’t work!  I am so confused as to why it isn’t working.  

My Code: 

[lua]     --search bar function to find user

   local function onSearchUser( ok, res, info )

        if (ok == true) then

           

           for key, value in ipairs( res.results ) do

           if (value.username == searchInput.text) then

                – user found!

print(“USER FOUND!”)

print( "Push notification target is: "…searchInput.text  )

gamesettings.pushNotificationTarget = “”…searchInput.text

loadsave.saveTable(gamesettings, “settings.json”)

else

native.showAlert( “No User Found”, searchInput.text…" does not exist!",  { “” }  )

end

      end

   end

end

  

  

  

  

  

  --search parse database for user by username

  parse.macro.findUserByUsername( ‘’…searchInput.text)

  :response(onSearchUser)[/lua]

I do not know the answer to your question, but for everyone else out there I did you the favor of “properly” indenting your code.  The code you posted is very difficult to read with the indentation you use.  

--search bar function to find user local function onSearchUser( ok, res, info ) if (ok == true) then for key, value in ipairs( res.results ) do if (value.username == searchInput.text) then -- user found! print("USER FOUND!") print( "Push notification target is: "..searchInput.text ) gamesettings.pushNotificationTarget = ""..searchInput.text loadsave.saveTable(gamesettings, "settings.json") else native.showAlert( "No User Found", searchInput.text.." does not exist!", { "" } ) end end end end --search parse database for user by username parse.macro.findUserByUsername( ''..searchInput.text) :response(onSearchUser)

@Jon

Thanks!  I didn’t want to say anything, but I wish everyone would go back and edit their code to make to nicely legible.   It isn’t enough to just dump into a code block.  Everyone should take the time to make their posts as legible and understandable as possible.

@mgold…,

Verify your code is hitting these parts,

--search bar function to find user local function onSearchUser( ok, res, info ) print("Entered onSearchUser()", ok, res, info ) if (ok == true) then print("IS OK") for key, value in ipairs( res.results ) do print( "key,value", key, value )

@roaminggamer

I tested out your code to verify what is happening in the function.  

When (value.username == searchInput.text), all 3 print statements you told me to implement fire correctly.

However, when the else statement is true, I get the first 2 print statements only.  

So from this testing, we know that the “for key, value in pairs( res.results) do” is not being called when the else statement is true.

I still can’t figure out why!

Any help?

Thanks.

@Mgoldberg62401, your test is bogus as if res.results is empty (careful, not nil; which is most probably the case when nothing is found) then you “else”  will never be taken.

Please always trace your code live (put a breakpoint and step your code and look at variable) and you should be able to find those kinds of problems in a few seconds. It help you and it help us to help you when its really a difficult problem to find or fix.

Hope this help

Nick

the do will not execute if there is nothing in 

res.results

Thus the if-then-else statement will not be reached.

Try this change to see what is in res.results

 if (ok == true) then print("IS OK res == ", res) if( res ) then print( "res.results == " res.results ) for k,v in pairs( res.results ) do print( "k,v == ", k, v ) end end for key, value in ipairs( res.results ) do print( "key,value", key, value )

@roaminggamer

I did what you told me to do. The first 2 print statements run fine.  In my console log, I get “res.results == table: 0x7fc8aa4614a0”

However, the third print statement and the others don’t run at all. 

How should I correct my code to allow the else statement to run?

Thanks.

Instead of:

for k,v in pairs( res.results ) do     print( "k,v == ", k, v ) end

which should have worked, try this instead:

local json = require("json") -- if you haven't done this already print( json.prettify ( res.results )

And see if that produces results for the contents of the res.results table.

Rob

@rob

That code you gave me works! I now see the full contents of the res.results table.  

When the username exists, the if statement works fine and the table is filled with correct information.  However, when the username doesn’t exist and the else statement should work, the table has no contents.

How can I change my else statement to run the code in it?

Thanks

@Mgoldberg62401, I gave you the recipe in my previous reply. If there is nothing to report, the table will be empty, but not nil. Simply test for the table if its empty or not. Also, please consider my advice and walk your code with the debugger, you would have find the solution in a minute. For the result, always first look on the parse website, everything is in there to explain how the data is return in all possible cases.

@nmichaud 

Sorry to ask this, how do i check to see if the table is empty? 

I honestly don’t know how to check.  Thanks

If it is numerically indexed:

if( #res.results ) print("no numeric indexes. may be empty") end

Alternately, count all fields and other entries:

local count = 0 for k,v in pairs( res.results ) do count = count + 1 end if( count == 0 ) print("Empty no matter how you look at it") end

@roaminggamer @nmichaud

Thanks for all your help! Works perfectly! 

@Mgoldberg62401, I am glad that it work for you, but as a recommendation, please read some LUA tutorials to learn this. This kind of test is basic knowledge in LUA, I would suggest that you Google in the future and again use a debugger, trace your code. This is the best way to learn and get experience faster.

next() is the most robust way, fe:

local function isNilOrEmpty(t) return (t==nil) or (next(t)==nil) end print( isNilOrEmpty(nil), "expect true" ) print( isNilOrEmpty({}), "expect true" ) print( isNilOrEmpty({nil}), "expect true" ) print( isNilOrEmpty({{}}), "expect false" ) print( isNilOrEmpty({1}), "expect false" ) print( isNilOrEmpty({key="value"}), "expect false" ) print( isNilOrEmpty({1,2,3,key="value",foo="bar"}), "expect false" )

You don’t need an elseif that is the opposite of the if.  Just use ‘else’

-- WRONG if (value.username == searchInput.text) then elseif (value.username ~= searchInput.text) then -- This could trigger false if both are nil. end -- RIGHT if (value.username == searchInput.text) then else end

@roaminggamer

I just tried exactly what you said, and the else statement won’t work!  I am so confused as to why it isn’t working.  

My Code: 

[lua]     --search bar function to find user

   local function onSearchUser( ok, res, info )

        if (ok == true) then

           

           for key, value in ipairs( res.results ) do

           if (value.username == searchInput.text) then

                – user found!

print(“USER FOUND!”)

print( "Push notification target is: "…searchInput.text  )

gamesettings.pushNotificationTarget = “”…searchInput.text

loadsave.saveTable(gamesettings, “settings.json”)

else

native.showAlert( “No User Found”, searchInput.text…" does not exist!",  { “” }  )

end

      end

   end

end

  

  

  

  

  

  --search parse database for user by username

  parse.macro.findUserByUsername( ‘’…searchInput.text)

  :response(onSearchUser)[/lua]