【问题标题】:Cloud function query conflict云函数查询冲突
【发布时间】:2020-07-07 00:59:18
【问题描述】:

我正在编写一个谷歌云功能来创建聊天室,用户从移动应用程序向他的兴趣发送请求到 firestore,然后触发函数读取 firestore 中所有待处理的请求,直到找到匹配用户的人。当发生这种情况时,云功能会删除两个请求并创建聊天室,但我的问题是,如果多个用户同时发送请求,云功能会读取理论上应该已经删除的请求。这是我的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();

exports.cargarPeticion = functions.firestore.document('/Requests/{id}').onCreate((snap, context) => {

    const newValue = snap.data();
    const uid = newValue.uid;
    const age = newValue.age;
    const sex = newValue.sex;
    const manInterest = newValue.Hombres;
    const womenInterest = newValue.Mujeres;
    const loveInterest = newValue.Love;
    const friendInterest = newValue.Friendship;
    const timestamp = newValue.Timestamp;
    const minAgeInterest = newValue.ageMin;
    const maxAgeInterest = newValue.ageMax;
    var ciclo = true;

    db.collection('Requests').orderBy('Timestamp').get()
    .then((snapshot) => {

        try{
            snapshot.forEach((doc) => {
                var data = doc.data(); 
                
                if(data.uid !== uid){
                    console.log("NUEVO");
                    console.log(doc.id, '=>', data.uid);

                    if(loveInterest === data.Love || friendInterest === data.Friendship){

                        if(data.age >= minAgeInterest &&
                            data.age <= maxAgeInterest && 
                            ((sex === "H" && data.Hombres === "true") || (sex === "M" && data.Mujeres === "true"))){

                            if(age >= data.ageMin &&
                                age <= data.ageMax && 
                                ((manInterest === "true" && data.sex === "H") || (womenInterest === "true" && data.sex === "M"))){

                                var arrayUsers = [uid, data.uid];

                            console.log("ID SALA");
                            console.log(uid.substring(0, 8) + data.uid.substring(0, 7) + doc.id.substring(0, 7));
                            db.collection('chatRoom').doc(uid.substring(0, 8) + data.uid.substring(0, 7) + doc.id.substring(0, 7)).set({
                                arrayUsers: arrayUsers,
                                uid1: uid,
                                uid2: data.uid,
                                chatRoomId : uid.substring(0, 8) + data.uid.substring(0, 7) + doc.id.substring(0, 7),
                                ultimoMensaje: '',
                                ultimoEscritorUid : '',
                                ultimoMensajeTiempo : 0
                            });

                            db.collection('Requests').doc(doc.id).delete();
                            db.collection('Requests').doc(snap.id).delete();


                            throw exception
                        }
                    }
                }
            }
        });
        }catch(exception){
            console.log(exception);
        }

        return null;
    })
    .catch((err) => {
        console.log('Error getting documents', err);
    });

})

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    为防止并发数据库更新创建不一致的数据集,您需要use a transaction 来执行更新。使用事务时,允许第一个实例修改数据,任何对同一数据的冲突更新都会自动重试,直到没有冲突为止。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-05
      • 2018-11-11
      • 2021-01-11
      • 2015-09-01
      • 2014-05-08
      • 1970-01-01
      • 2017-04-18
      相关资源
      最近更新 更多