简体   繁体   English

Swift - Firestore 自动创建集合/文档

[英]Swift - Firestore auto create collection/document

Is there a way to create a collection or document automatically?有没有办法自动创建一个集合或文档?

In my case I already have a collection ("users") and in it are all of the the users.就我而言,我已经有了一个collection (“用户”),其中包含所有用户。 What I would like to achieve is if the user clicks on a button several subcollections and also an empty document should be created.我想要实现的是,如果用户单击一个按钮,则会创建几个subcollections ,并且还应创建一个空document

I tried this but is failing:我试过这个但失败了:

func insertWish(){
    let db = Firestore.firestore()
    let userID = Auth.auth().currentUser!.uid
    db.collection("users").document(userID).collection("wishlists").document("Main Wishlist").collection("Wünsche").document("Laptop").setData(["": ""]) { (error) in
        if error != nil {
            print("error")
        }
    }
}

These collections/document do not exist in my database and should only be created if the user clicks the button:这些集合/文档在我的数据库中不存在,只有在用户单击按钮时才应创建:

collection("wishlists").document("Main Wishlist").collection("Wünsche").document("Laptop")

The error message "The property.name is the empty string" is telling you that you're trying to add an invalid document:错误消息“The property.name is the empty string”告诉您您正在尝试添加无效文档:

setData(["": ""])

A field name of empty string is not valid.空字符串的字段名称无效。 Give it a name that conforms to the requirements in the documentation .取一个符合文档要求的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM