Hi,
How are you doing? Im new to Backendless and I really like your interface. However, I have some struggles. The problem is about registering user and getting the response. I use Swift.
Im trying to connect a User to a Person.
First, I was getting the user response as nil. Then, I solved the problem (don’t know how ). I can add a User, a Person and add User to that Person.
Now, when I try to save another user (with different email - identity) after I create one, I get the User as nil.
Thank you in advance.
Görkem
P.S.: Code is attached, as well.
// Registers user to Backendless
private func registerUser() {
let user = BackendlessUser()
user.name = self.nameTextField.text! as NSString
user.email = self.emailTextField.text! as NSString
user.password = self.passwordTextField.text! as NSString
backendless.userService.register(user, response: { (newUser) in
self.addUserAsPerson(user: newUser)
self.informationLabel.text = "User registered successfully."
}) { (fault) in
print(fault!)
self.informationLabel.text = "Error: \(String(describing: fault?.description))"
}
}
// Adds user to People table
private func addUserAsPerson(user:BackendlessUser?) {
print("userId: \(user?.objectId)")
let newPerson = People()
newPerson.birthYear = Int(self.birthyearTextField.text!) as NSNumber?
let dataStore = backendless.data.ofTable("People")
dataStore?.save(newPerson,
response: {
(result) -> () in
backendless.data.of(People.ofClass()).setRelation("user:Users:1", parentObjectId: (result as! People).objectId, childObjects: [user?.objectId as Any], response: { response) in
}, error: { (fault) in
print(fault!)
})
},
error: {
(fault : Fault?) -> () in
print(fault!)
})
}
// Console Output:
userId: Optional(DB774EE6-4457-95FF-FF83-2692EBA83100)
userId: nil
FAULT = '1303' [Unable to create relation. Child object with id 'null' is not found in the related table.] <Unable to create relation. Child object with id 'null' is not found in the related table.>
Could you please let me know where the second “userId: nil” in the console output comes from?
If that objectId is indeed nil, it explains why setting the relationship fails, but by looking at the code, I see that there should be only one line printed out with the user’s objectId.
When I register user, registerUser() function is called. In the view attached, when I click on save, it registers User, creates a Person and adds that user to the Person created (prints userId as seen - console output #2). Then, I change the inputs (on the view), and click save again. So the function is called twice. During the second registration, it creates another User with the new e-mail addres (id), another Person but, because the response user is nil (prints nil - console output #3), it cannot add the 2nd User to 2nd Person. And we got an error (console output #4).
Since you get nil from user registration, we should focus on that issue, since that’s not the expected response. The fact that relation is not created is a negative side-effect of a previously failed operation.
Could you please isolate the use-case when the returned value is nil?
I didn’t understand the isolation of use-case. Here is another log, maybe it will help.
-------
new User is created: Optional(22AB4F23-B8B2-B6B7-FF13-3B5951AA1800)
new person is created: Optional("D48F2FD8-C3F9-AF65-FFEE-FCAACF19FA00")
user-person relationship is created between user: Optional(22AB4F23-B8B2-B6B7-FF13-3B5951AA1800) and person: Optional("D48F2FD8-C3F9-AF65-FFEE-FCAACF19FA00"))
-------
-------
new User is created: nil
new person is created: Optional("6EC9B1D1-ABBD-DF65-FFE2-A0CFC15B2900")
user-person relationship couldn't be created.
-------
FAULT = '1303' [Unable to create relation. Child object with id 'null' is not found in the related table.] <Unable to create relation. Child object with id 'null' is not found in the related table.>
Unfortunately, it does not help because it is not clear how you’re creating the user when you get back null. What is different about the second time you call the method?
If you could zip up the project directory and share it with us, we’d be able to debug it on our side. Please upload the zip file to either google drive or dropbox and email the link to support@backendless.com