Search code examples
iosswiftparse-platformpfquerypfuser

Unable to get currentUser PFUser object from Parse.com


As you can see in the screenshot below, I have three users with usernames user1, user2 and user3 in the User table. I am trying to use the code below to get (so that I can update later) the User object with username = "user1". I dont get anything, I just get error.

enter image description here

When the function below is run, this is printed:

saveUserSalaryInfo() - found NOOO-NIL user matching pfquery criteria

This shows that it is always getting the error. Why? What am I doing wrong?

Code:

func saveUserSalaryInfo() {

    if PFUser.currentUser() != nil {
        var query = PFQuery(className:"User")
        query.whereKey("username", equalTo: "user1")
        query.findObjectsInBackgroundWithBlock {
            (users: [AnyObject]?, error: NSError?) -> Void in
            if error != nil {

                NSLog("saveUserSalaryInfo() - found some user matching pfquery criteria")

            }

            else {
                NSLog("saveUserSalaryInfo() - found NOOO-NIL user matching pfquery criteria")
            }
        }

    } else {
        NSLog("found error - current User is not logged in")
    }

}

Solution

  • You are doing the test the other way around, if the error is nil that means no error happened

    if error == nil {
        NSLog("No error")
    } else {
        NSLog("Error")
    }
    

    Or to check if there is data use:

    if users != nil {
        NSLog("Find Users")
    } else {
        NSLog("Didn't find users")
    }