简体   繁体   中英

The user uid error! (Swift and Firebase Project)

I'm working on iOS application using swift and firebase.

I'm having a view called add device, where the registered user can add a device. Simply he/she can enter the device name, description, category and add the image then click on add device button.

Here's the current dashboard that contains the info of the registered users:

{
"UserProfile" : {
"1EJ8QmEQBJfBez7PMADbftCjVff1" : {
  "city" : "الرياض",
  "email" : "noura@gmail.com",
  "name" : "Norah",
  "phone" : "0564695353"
},
"97PUAcUC5UYxLpBOLnC4yQjxiEf2" : {
  "city" : "Riyadh",
  "email" : "mawada2017@gmail.com",
  "name" : "Mawada",
  "phone" : "0534260282"
  }
 }
}

I want to add the device info into another node.

Here's the button:

     @IBAction func AddDeviceButton(sender: AnyObject) {

    if DeviceName.text == "" || Description.text == "" || ImageView.image == nil {
        let alert = UIAlertController(title: "عذرًا", message:"يجب عليك تعبئة معلومات الجهاز كاملة", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "نعم", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}

    } else {


        let imageName = NSUUID().UUIDString
        let storageRef = FIRStorage.storage().reference().child("Devices_images").child("\(imageName).png")

        if let uploadData = UIImagePNGRepresentation(self.ImageView.image!) {

            storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in

                if error != nil {
                    print(error)
                 return
                }

                let profileImageUrl = metadata?.downloadURL()?.absoluteString

                 let DeviceInfo = [
                        "ImageUrl":profileImageUrl!,
                        "DeviceName":self.DeviceName.text!,
                        "Description":self.Description.text!,
                        "Category":self.itemSelected
                        ]

                    self.ref.child("Devices").child(user!.uid).setValue(DeviceInfo)

            })






}

}

In this line:

  self.ref.child("Devices").child(user!.uid).setValue(DeviceInfo)

The view could not recognize the uid!

Why?

Just replace

self.ref.child("Devices").child(user!.uid).setValue(DeviceInfo)

with

self.ref.child("Devices").child(FIRAuth.auth()!.currentUser!.uid).setValue(DeviceInfo)

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