Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 云函数查询冲突_Node.js_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Node.js 云函数查询冲突

Node.js 云函数查询冲突,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我正在编写一个谷歌云函数来创建聊天室,用户从移动应用程序向firestore发送一个请求,请求中包含他的兴趣,然后触发函数读取firestore中所有未决的请求,直到找到与用户匹配的人。当这种情况发生时,云函数将删除这两个请求并创建聊天室,但我的问题是,如果多个用户同时发送请求,云函数将读取理论上应该已经删除的请求。这是我的代码: const functions = require('firebase-functions'); const admin = require('firebase-ad

我正在编写一个谷歌云函数来创建聊天室,用户从移动应用程序向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);
    });

})
const functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp();
const db=admin.firestore();
exports.cargarPeticion=functions.firestore.document('/Requests/{id}').onCreate((快照,上下文)=>{
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.友谊;
const timestamp=newValue.timestamp;
const minAgeInterest=newValue.ageMin;
const maxAgeInterest=newValue.ageMax;
var ciclo=真;
db.collection('Requests').orderBy('Timestamp').get()
。然后((快照)=>{
试一试{
snapshot.forEach((doc)=>{
var data=doc.data();
如果(data.uid!==uid){
控制台日志(“新”);
console.log(doc.id'=>',data.uid);
如果(loveInterest==data.Love | | friendInterest==data.friendy){
如果(data.age>=minAgeInterest&&
data.age=data.ageMin&&
年龄{
console.log('获取文档时出错',err);
});
})

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