【问题标题】:Firestore batch set is only inserting the last itemFirestore 批处理集仅插入最后一项
【发布时间】:2019-02-01 20:13:47
【问题描述】:

我正在尝试从数组项批量创建一堆文档,其中每个项都是键值对的映射。我的数组中有多个值,但由于某种原因,唯一插入到数据库中的项是数组中的最后一项。

我在这里做错了什么?或者我可以在提交历史中只使用一次 setData 吗?

let wordsRef = db.collection("items").document()

for item in items {
    batch.setData(item, forDocument: wordsRef)
}

batch.commit() { err in
    // error reporting
}

谢谢大家!

【问题讨论】:

    标签: swift firebase google-cloud-firestore swift4


    【解决方案1】:

    前段时间我也遇到了同样的问题。只需简单地将 wordsRef 移动到 for 循环中,就像我在下面所做的那样。希望这会有所帮助...

    代码:

    for item in items {
        let wordsRef = db.collection("items").document()
        batch.setData(item, forDocument: wordsRef)
    }
    
    batch.commit() { err in
        // error reporting
    }
    

    【讨论】:

    • 是的,这样每个文档都会有不同的 ID!谢谢你。
    • 没问题,很高兴能帮上忙! ?
    猜你喜欢
    • 2017-01-07
    • 2013-09-19
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2017-04-01
    相关资源
    最近更新 更多