IdaEmilie
IdaEmilie

Reputation: 85

Check if Parse.com column Array contains current username

I am trying to check if the array in parse contains the current users username:

var thisUser = PFUser.currentUser()?.username

Query:

var query = PFQuery(className: "currentUploads")
            query.whereKey("reportedBy", containsString: self.thisUser)
            query.findObjectsInBackgroundWithBlock({ (object: [AnyObject]?, error: NSError?) -> Void in
                if (error == nil){
                    // Success fetching user in reportedBy field
                    println("User found")
                }
                else
                {
                    println(error)
                    println("User NOT found")
                }
            })

enter image description here

It gives me error:

[Error]: $regex only works on string fields (Code: 102, Version: 1.8.0) Optional(Error Domain=Parse Code=102 "$regex only works on string fields" UserInfo=0x7fba6a5b7610 {code=102, temporary=0, error=$regex only works on string fields, NSLocalizedDescription=$regex only works on string fields}) User NOT found

Any suggestions?

Upvotes: 0

Views: 209

Answers (2)

Mo Nazemi
Mo Nazemi

Reputation: 2717

Given that you should use equalTo instead of containsStringIt, it will not be weird that you get an empty object. That should be correct if your username is not in any of the arrays. I think you believe if no record is found, you should get an error but that is not the case. You simply get an empty object back if nothing is found.

Upvotes: 0

Christian
Christian

Reputation: 22343

You have to use equalTo instead of containsString. As the error says, containsString only works with Strings and not with Arrays:

query.whereKey("reportedBy", equalTo: self.thisUser)

The name is missleading but the equalTo checks all array-elements if they are equal to your string.

Upvotes: 1

Related Questions