简体   繁体   中英

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.

在此处输入图片说明

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")
    }

}

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")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM