控制台错误:无效的Firebase数据设置值无法存储对象

时间:2018-07-21 03:36:32

标签: ios firebase firebase-realtime-database swift3 firebase-storage

我是xcode 8的初学者,很快,在我的控制台中遇到以下错误,请帮助我

由于未捕获的异常'InvalidFirebaseData'而终止应用程序,原因:'(setValue :)无法在profileImageUrl中存储_SwiftValue类型的对象。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象。'

这是我的代码,

let uid = user?.user.uid
            let storageRef = Storage.storage().reference(forURL: "gs://instagramclone-7713a.appspot.com").child("profile_image").child(uid!)
            if let profileImg = self.selectedImage, let imageData = UIImageJPEGRepresentation(profileImg, 0.1) {
                storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
                    if (error != nil) {
                        return
                    }

                    let profileImageUrl = storageRef.downloadURL(completion: { url, error in

                    })

                    let ref = Database.database().reference()
                    let usersReference = ref.child("users")
                    let newUserReference = usersReference.child(uid!)
                    newUserReference.setValue(["username": self.UsernameTextField.text!, "email": self.emailTextField.text!, "profileImageUrl": profileImageUrl])
                })
            }

        })

    }

1 个答案:

答案 0 :(得分:0)

您必须在setValue()的完成块中调用downloadURL()

storageRef.downloadURL(completion: { url, error in
    let ref = Database.database().reference()
    let usersReference = ref.child("users")
    let newUserReference = usersReference.child(uid!)
    newUserReference.setValue(["username": self.UsernameTextField.text!, "email": self.emailTextField.text!, "profileImageUrl": url])
})