0

Okay I have a classNamed: "PrivateGroups". Each row in PrivateGroups contains "GroupName" and "Password". In a UIView, if the user enters the GroupName and Password and hits submit, I want to add their user objectId to that specific groupname/password entry under "members".

This is the code I have that isn't working:

var query = PFQuery(className: "PrivateGroups")

query.whereKey("privatename", equalTo: joinname.text)

query.whereKey("password", equalTo: joinpassword.text)

query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

if objects.count == 1 {

for object in objects {

query.getFirstObject().addUniqueObject(PFUser.currentUser().objectId, forKey: "members")

query.getFirstObject().saveInBackground() }}

This is what the className looks like

so if you match the "members" and "password" and press the button, i want to add the user's object id to "members"

1 Answer 1

1

In your loop for object in objects you shouldn't need to also call query.getFirstObject() since you already have the object. Instead, try

for object in objects {
       if let arr = object["members"]{
          if(arr.containsObject(PFUser.currentUser().objectID == false){
             arr.addObject(PFUser.currentUser().objectID)
             object.saveInBackground()
          }
       }   
}
Sign up to request clarification or add additional context in comments.

4 Comments

oh my bad, that isn't the issue in my project i use query1, throughout. i edited the post to reflect that. any clue?
I thought that my just be a typo! Just to make sure, your "members" column is declared as an array in Parse, right? Also, are you getting an error message when running the code?
Get a error, Cannot invoke 'add unique object' with and argument list type (String!)
Sorry, forgot addUniqueObject takes more than just one input. Changed that in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.