【问题标题】:Why this batch transcation in failing in Cloud Firestore?为什么此批处理事务在 Cloud Firestore 中失败?
【发布时间】:2020-07-02 21:07:44
【问题描述】:

我正在为我的应用程序使用 React Native,我正在执行一个批处理事务,但在我设置的第二个批处理中失败了:

let rriNshpiUserCollection = await firestore().collection('myDoc').doc(login_uid);
    let privateDoc = await firestore().collection('myDoc').doc(login_uid).collection('private');
    let batch = firestore().batch();

        batch.update(rriNshpiUserCollection, {
            addCoord: this.state.location,
            last_location: this.state.location,
            device_id: this.state.device_id,
            update_time: timeStamp,
            act: 1
        });
        batch.set(privateDoc, { // its failing here
            loc: this.state.location,
            time: timeStamp
        });

错误:

错误:firebase.firestore.batch().set(*) 'documentRef' 预期的 DocumentReference 实例。

你能说出这段代码有什么问题吗?

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore


    【解决方案1】:

    错误来自此代码:

    batch.set(privateDoc, {
        loc: this.state.location,
        time: timeStamp
    });
    

    原因是privateDoc是对集合的引用,不能对集合调用set(或对集合调用batch.set)。

    如果您想将新文档添加到 private 集合中,您可以为此创建一个引用:

    batch.set(privateDoc.doc(), {
        loc: this.state.location,
        time: timeStamp
    });
    

    或者(可能更好):

    let privateDoc = firestore().collection('myDoc').doc(login_uid)
                                .collection('private').doc();
    batch.set(privateDoc, {
        loc: this.state.location,
        time: timeStamp
    });
    

    【讨论】:

      【解决方案2】:

      您的 privateDoc 是 CollectionReference,但错误消息告诉您需要 DocumentReference。事务必须对单个文档进行操作。

      【讨论】:

        猜你喜欢
        • 2019-07-26
        • 2018-07-28
        • 2019-05-27
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多