danutha danutha - 9 months ago 70
Swift Question

Exception when trying to save user email and name in to the firebase DB

Im working on a project in swift 3.0 and I'm getting the following exception when trying to save two string values in to the firebase DB.

erminating app due to uncaught exception 'InvalidFirebaseData', reason: '(updateChildValues:withCompletionBlock:) Cannot store object of type _SwiftValue at . Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.'

The code of the class as bellow. What am I missing ?

import UIKit
import Firebase

class ViewController: UIViewController {
@IBOutlet weak var userNameTextField: UITextField!
@IBOutlet weak var userEmailTextField: UITextField!
@IBOutlet weak var passWordTextField: UITextField!

override func viewDidLoad() {
@IBAction func loginPressed(_ sender: AnyObject) {
FIRAuth.auth()?.createUser(withEmail: userEmailTextField.text!, password: passWordTextField.text!, completion: { (user :FIRUser?, error) in
if error != nil {

print("error in login")
print("loggin success")

let ref = FIRDatabase.database().reference(fromURL: "https://chatsample-4cc11.firebaseio.com/")
let values = ["username" : self.userNameTextField.text, "email" : self.userEmailTextField.text]
ref.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print("saved user successfully to firebase DB")
//ref.updateChildValues(["somevalue": 123123])



Answer Source

I think the problem is you didn't unwrap the values of username and email, so these values were type of Optional. You should change to

let values = ["username" : self.userNameTextField.text!, "email" : self.userEmailTextField.text!]