简体   繁体   中英

Can't add user to existing Parse.com role

With this code, I try to add user to existing Parse.com role "banned":

 var roleACL = PFACL()
 var role = PFRole(name: "banned", acl:roleACL)
 role.users.addObject(userObject) // This should add user
 role.saveInBackground()

I get error 137 A duplicate value for a field with unique values was provided (Code: 137, Version: 1.6.1)

I guess it tries to recreate the role. However, form the doc, at https://www.parse.com/docs/ios_guide#roles-security/iOS I don't see how i can add a user to an existing role.

Got the solution. The misleading code above, as is on Parse.com site tries to recreate a role. The correct code is :

var queryRole = PFRole.query() // You need to get role object
queryRole.whereKey("name", equalTo:"banned")
queryRole.getFirstObjectInBackgroundWithBlock() {
(roleObject: PFObject!, error: NSError!) -> Void in
if error == nil {

      // Assign user to banned role
      var roleToAddUser = roleObject as PFRole
      roleToAddUser.users.addObject(userObject)
      roleToAddUser.saveInBackground()

     }
}

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