Start a new topic
Answered

KCSUserDiscovery - Mobile Taken

Hello, 


First post in Kinvey and hope this hasn't been answered before. I am working on my app now and figuring our the registration part. When the user fill the form (providing during the process a username and mobile), I want to check in the backend if the username is taken or not. I also want to check if the mobile number is taken or not (each mobile number should be mapped to one account only). 


To accomplish the above, I did the following for username:

 

func isUsernameAvailable(input: String) -> Void {
        let potentialUsername = input
        KCSUser.checkUsername(
            potentialUsername,
            withCompletionBlock: { (username: String!, usernameAlreadyTaken: Bool, error: NSError!) -> Void in
                if usernameAlreadyTaken {
                   self.usernameErrorLabel.text = "Username taken. Please try another"
                }else{
                    
                    self.isMobileRegistered(self.mobileTextField.text!)
                }
            }
        )
    }

 Then for the mobile function embedded here, I did this:


  

func isMobileRegistered(input:String)->Void{
        let mobile = input
        KCSUserDiscovery.lookupUsersForFieldsAndValues(
            [ "mobile" : "01111102"],
            completionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in
                if errorOrNil == nil {
                    //array of matching KCSUser objects
                    NSLog("Found %d mobile", objectsOrNil.count)
                } else {
                     self.usernameErrorLabel.text = ""
                     self.userInstance.email = self.emailTextField.text
                     self.userInstance.mobile = self.mobileTextField.text
                     self.userInstance.username = self.usernameTextField.text
                     self.userInstance.password = self.passwordTextField.text
                     self.userInstance.name = self.nameTextField.text
                     self.performSegueWithIdentifier("mobileVerification", sender: self)
                }
            },
            progressBlock: nil
        )
    } 

 

Now what I expect to happen is that the "if" condition of the mobile function will be triggered because there is actually a user with mobile number 011111102 registered already. What happens instead is that the else clause is executed and segue is executed. How come this is happening?


Thanks in advance


Best Answer
Khalid,

As per the documentation, these are the only valid attribute keys supported.
http://devcenter.kinvey.com/ios/reference/api/Classes/KCSUserDiscovery.html

I would suggest writing a custom endpoint which accepts the mobile number to be searched as an input and then in the endpoint query the user collection for this mobile number and then return the appropriate JSON back.
http://devcenter.kinvey.com/ios/guides/business-logic#types


Thanks,
Pranav
Kinvey Support

 


Khalid,


Does the lookupUsersForFieldsAndValues function work for you for any of the following keys:

  • KCSUserAttributeUsername
  • KCSUserAttributeSurname
  • KCSUserAttributeGivenname
  • KCSUserAttributeEmail


and doesn’t work for the custom key that you have specified?

Here are links for more info:
http://devcenter.kinvey.com/ios/reference/api/Classes/KCSUserDiscovery.html
http://devcenter.kinvey.com/ios/reference/api/Classes/KCSUser.html



Thanks,

Pranav

Kinvey Support


1 person likes this

I tried using the KCSUserAttributeEmail and it detected that the same email exists in database. But when I try with same syntax but change field to mobile I get an error. Here is the code I am trying now and the error too..:



 

 KCSUserDiscovery.lookupUsersForFieldsAndValues(
            [ "mobile" : "0549135363" ],
            completionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in
                if errorOrNil == nil {
                    //array of matching KCSUser objects
                    NSLog("Found %d Smiths", objectsOrNil.count)
                } else {
                    NSLog("Got An error: %@", errorOrNil)
                }
            },
            progressBlock: nil
        )

 And the error code I get is:


 

2016-07-01 00:05:31.994 appName[6924:1860821] Kinvey Ping Success
All Input Validation Successful
2016-07-01 00:05:51.212 appName[6924:1860821] Got An error: Error Domain=KCSServerErrorDomain Code=400 "The query string in the request has an invalid syntax" UserInfo={NSLocalizedDescription=The query string in the request has an invalid syntax, NSErrorFailingURLKey=https://baas.kinvey.com/user/jfdkjfkdjfd/_lookup, Kinvey.kinveyErrorCode=InvalidQuerySyntax, KinveyKit.HTTPMethod=POST, Kinvey.RequestId=83948wjd9jd9j39jd39jd3, NSLocalizedFailureReason=not allowed to invoke _lookup on attribute mobile}

 Still unable to fix this simple issue...

Any update here? Still unable to fix...

Answer
Khalid,

As per the documentation, these are the only valid attribute keys supported.
http://devcenter.kinvey.com/ios/reference/api/Classes/KCSUserDiscovery.html

I would suggest writing a custom endpoint which accepts the mobile number to be searched as an input and then in the endpoint query the user collection for this mobile number and then return the appropriate JSON back.
http://devcenter.kinvey.com/ios/guides/business-logic#types


Thanks,
Pranav
Kinvey Support

 

Login or Signup to post a comment