Home > Software engineering >  Adding a field to firebase firestore database in Swift
Adding a field to firebase firestore database in Swift

Time:03-20

I am trying to add a new field (hashtags) to my service struct and get an error in Xcode. "Cannot find 'hashtags' in scope" below is my code.

import SwiftUI
import Firebase
import FirebaseFirestoreSwift

struct FeedTry: Identifiable, Decodable {
    @DocumentID var id: String?
    let caption: String
    let myhashtags: String  //Added this new field
    let timestamp: Timestamp
    let uid: String
    var likes: Int
    var saves: Int
    
    var user: User?
    var didLike: Bool? = false
    var didSave: Bool? = false
}

Below is my service view

struct UploadTryService { 
    func uploadTry(caption: String, completion: @escaping(Bool) -> Void) {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        
        let data = ["uid": uid,
                    "caption": caption,
                    "hashtags": myhashtags, //getting the error here
                    "likes": 0,
                    "saves": 0,
                    "timestamp": Timestamp(date: Date())
        ] as [String : Any]
        
        Firestore.firestore().collection("try").document().setData(data) { error in
            if let error = error {
                print("DEBUG: Failed to upload try with error: \(error.localizedDescription)")
                completion(false)
                return
            }
            
            completion(true)
        }
    }


}

Checking online and in the documentation, I haven't found a way to fix it.

CodePudding user response:

Just like you did with caption, you can pass a parameter into uploadTry with a value for myhashtags:

struct UploadTryService { 
    func uploadTry(caption: String, myhashtags: String, completion: @escaping(Bool) -> Void) { //<-- Here
        guard let uid = Auth.auth().currentUser?.uid else { return }
        
        let data = ["uid": uid,
                    "caption": caption,
                    "myhashtags": myhashtags, //getting the error here
                    "likes": 0,
                    "saves": 0,
                    "timestamp": Timestamp(date: Date())
        ] as [String : Any]
        
        Firestore.firestore().collection("try").document().setData(data) { error in
            if let error = error {
                print("DEBUG: Failed to upload try with error: \(error.localizedDescription)")
                completion(false)
                return
            }
            
            completion(true)
        }
    }
}

That means at your calcite, you'd need to include it as well:

uploadTry(caption: "Caption", myhashtags: "Hashtags here", ...)

Another option (depending on what you're looking for) would be to just pass in an empty String:

let data = ["uid": uid,
                    "caption": caption,
                    "myhashtags": "",
                    "likes": 0,
                    "saves": 0,
                    "timestamp": Timestamp(date: Date())

Note that in both cases I changed the dictionary key to myhashtags since that's what you named it in your model. You can choose either key -- just make sure they match.

  • Related