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 firestore云函数缺少一些onCreate文档_Node.js_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Node.js firestore云函数缺少一些onCreate文档

Node.js firestore云函数缺少一些onCreate文档,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我正在尝试显示一个用户,他的手机联系人也是我的应用程序的用户 我正在Firestore“联系人”集合中存储用户电话联系人 在android上创建的每个文档中,我都将“fuid”(frienduid)字段保留为null。使用云功能,每次创建新文档时,我都会检查他的“paPho”(解析电话号码,例如+972123455)是否与现有用户电话号码匹配。如果是,我将把他的uid放在“fuid”匹配文档中 在android上,我将显示fuid不为null且uid匹配的所有用户联系人 因为每个用户可能有500

我正在尝试显示一个用户,他的手机联系人也是我的应用程序的用户

我正在Firestore“联系人”集合中存储用户电话联系人

在android上创建的每个文档中,我都将“fuid”(frienduid)字段保留为null。使用云功能,每次创建新文档时,我都会检查他的“paPho”(解析电话号码,例如+972123455)是否与现有用户电话号码匹配。如果是,我将把他的uid放在“fuid”匹配文档中

在android上,我将显示fuid不为null且uid匹配的所有用户联系人

因为每个用户可能有500多个联系人(全部在很短的时间内添加),所以我使用Blaze plan

它工作得很好,但是,尽管在日志中没有发现错误,有时似乎缺少onCreate。

我认为它丢失的原因是,如果我在同一联系人列表下重新运行云功能几次,有时会出现丢失的文档

这可能与这些有时丢失的联系人姓名相近且电话号码相同有关

const functions = require('firebase-functions');

exports.attachUserToNewContact = functions.firestore
    .document('contacts/{contactId}').onCreate((snap,contex) => {

        admin.auth().getUserByPhoneNumber(snap.data().paPho)
            .then(userRecord => {
                if (userRecord.uid) {
                    console.log(`userRecord phone ${snap.data().paPho} matching contact ${userRecord.uid}`);
                    admin.firestore().collection('contacts')
                    .doc(snap.data().id).update({fuid:userRecord.uid});
                }

                return 0;
            })
            .catch(error => {
                //There is no user record corresponding to the provided identifier
            });

        return 0;
    });

您没有返回异步方法(
getUserByPhoneNumber()
update()
)返回的承诺,这可能会导致云函数出现一些“不稳定”的行为

正如您将在官方Firebase视频系列()中关于“JavaScript承诺”的三个视频中看到的,您必须在后台触发的云函数中返回承诺或值,以向平台表明它已完成,并避免在异步操作完成之前终止

具体地说,有时,您的云函数在异步操作完成之前终止,因为
返回0向云功能平台表示可以终止该功能。另一方面,云功能平台不会立即终止功能,异步操作可以完成

通过按如下方式修改代码,您将避免这种“不稳定”的行为:

const functions = require('firebase-functions');

exports.attachUserToNewContact = functions.firestore
    .document('contacts/{contactId}').onCreate((snap,contex) => {

        return admin.auth().getUserByPhoneNumber(snap.data().paPho)
            .then(userRecord => {
                if (userRecord.uid) {
                    console.log(`userRecord phone ${snap.data().paPho} matching contact ${userRecord.uid}`);
                    return admin.firestore().collection('contacts')
                    .doc(snap.data().id).update({fuid:userRecord.uid});
                } else {
                    return null;
                } 
            })
            .catch(error => {
                //There is no user record corresponding to the provided identifier
                return null;
            });

    });

顺便说一句,如果您除了在
catch
块中返回
null
之外不做任何事情,那么您可以完全删除它

您没有返回异步方法(
getUserByPhoneNumber()
update()
)返回的承诺,这可能会导致云函数的某些“不稳定”行为

正如您将在官方Firebase视频系列()中关于“JavaScript承诺”的三个视频中看到的,您必须在后台触发的云函数中返回承诺或值,以向平台表明它已完成,并避免在异步操作完成之前终止

具体地说,有时,您的云函数在异步操作完成之前终止,因为
返回0向云功能平台表示可以终止该功能。另一方面,云功能平台不会立即终止功能,异步操作可以完成

通过按如下方式修改代码,您将避免这种“不稳定”的行为:

const functions = require('firebase-functions');

exports.attachUserToNewContact = functions.firestore
    .document('contacts/{contactId}').onCreate((snap,contex) => {

        return admin.auth().getUserByPhoneNumber(snap.data().paPho)
            .then(userRecord => {
                if (userRecord.uid) {
                    console.log(`userRecord phone ${snap.data().paPho} matching contact ${userRecord.uid}`);
                    return admin.firestore().collection('contacts')
                    .doc(snap.data().id).update({fuid:userRecord.uid});
                } else {
                    return null;
                } 
            })
            .catch(error => {
                //There is no user record corresponding to the provided identifier
                return null;
            });

    });
顺便说一句,如果您除了在
catch
块中返回
null
之外不做任何事情,那么您可以完全删除它