Node.js 在firestore get查询中使用通配符

Node.js 在firestore get查询中使用通配符,node.js,firebase,google-cloud-firestore,google-cloud-functions,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,我想在firebase中创建一个云函数,每当用户第一次登录时就会触发该函数。该函数需要将特定用户身份验证中的UID添加到firestore中已存在的特定文档中。问题是需要将UID添加到我不知道其位置的文档中。我现在的代码并没有完全做到这一点,但这是它出错的地方。简化后的数据库如下所示 organisations [randomly generated id] people [randomly generated id] (in here, a s

我想在firebase中创建一个云函数,每当用户第一次登录时就会触发该函数。该函数需要将特定用户身份验证中的UID添加到firestore中已存在的特定文档中。问题是需要将UID添加到我不知道其位置的文档中。我现在的代码并没有完全做到这一点,但这是它出错的地方。简化后的数据库如下所示

organisations
    [randomly generated id]
        people
            [randomly generated id]  (in here, a specific document needs to be found based on known email 
                                      adress)
存在多个不同的组织,用户所属的组织未知。我考虑过使用通配符,如下所示:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
console.log('function ready');
//Detect first login from user
//if(firebase.auth.UserCredential.isNewUser()){
if(true){
    //User is logged in for the first time
    //const userID = firebase.auth().currentUser.UID;
    //const userEmail = firebase.auth().currentUser.email;
    const userID = '1234567890';
    const userEmail = 'example@example.com';
    //Get email, either personal or work
    console.log('Taking a snapshot...');
    const snapshot = db.collection('organisations/{orgID}/people').get()
        .then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
            console.log(doc.data());
        });
    });
}
出于测试目的,我注释了一些基于身份验证的行。我知道代码仍然在运行,因为硬编码orgID确实返回正确的值。此外,通过每个组织循环不是一个选项,因为我需要有可能有很多组织

很多解决方案都是基于firestore触发器的,比如onWrite,您可以使用这样的通配符。 然而,我认为这在这种情况下是不可能的

上述问题的解决方案:

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

//Add UID to document in DB[FMIS-94]
//Detect first login from user
//if(firebase.auth.UserCredential.isNewUser()){
  if(true){
    //User is logged in for the first time
    //const userID = firebase.auth().currentUser.UID;
    //const userEmail = firebase.auth().currentUser.email;
    const userID = '1234567890';
    const userEmail = 'example@example.com';
    var docFound = false;
    //Get email, either personal or work
    console.log('Taking a snapshot...');
    //Test for work email
    const snapshot = db.collectionGroup('people').where('email.work', '==', userEmail).get()
      .then(function(querySnapshot){
        querySnapshot.forEach(function(doc){
          //work email found
          console.log('work email found');
          console.log(doc.data()); 
          docFound = true;
          const organisationID = doc.ref.parent.parent.id;
          writeUID(doc.id, userID, organisationID);  
        });
      });
  
    if(!docFound){
      //Test for personal email
      const snapshot = db.collectionGroup('people').where('email.personal', '==', userEmail).get()
      .then(function(querySnapshot){
        querySnapshot.forEach(function(doc){
          //personal email found
          console.log('personal email found');
          console.log(doc.data()); 
          const organisationID = doc.ref.parent.parent.id;
          writeUID(doc.id, userID, organisationID);  
        });
      });
    }
  }
  async function writeUID(doc, uid, organisationID){ 
    const res = db.collection(`organisations/${organisationID}/people`).doc(doc).set({
      userId: uid
    }, { merge: true });  
  } 

这正是我所需要的,谢谢大家的帮助

第一次设置云功能是根据

然后在设置如下创建函数后:

exports.YOURFUNCTIONNAME= functions.firestore
 .document('organisations/[randomly generated id]/people/[randomly generated id]')
 .oncreate(res => {

    const data = res.data();
    const email = data.email;/----Your field name goes here-----/

     /-----------------Then apply your logic here---------/

 )}

无论何时创建People->Random ID,都会触发该函数。当用户登录到前端应用程序时,不可能触发云函数。在这些国家中没有这样的触发因素

如果您想根据用户的某些特征(uid或电子邮件)更新文档,您可以在用户登录后通过应用程序进行更新

您在问题中提到,“在这里,需要根据已知的电子邮件地址找到特定的文档。”。您应该首先构建一个查询来查找此文档,然后更新它,所有这些都来自应用程序



另一个经典方法是为每个用户创建一个特定的文档,该文档使用用户uid作为文档ID,例如在
users
集合中。识别/查找此文档非常容易,因为一旦用户登录,您就知道他的uid。

我不确定我是否正确理解您的意思,但是如果您想搜索所有
集合,不管他们在哪个
组织
文档下,解决方案是使用集合组查询

db.collectionGroup('people').get()
    .then(function(querySnapshot) {    
       querySnapshot.forEach(function(doc) {
        console.log("user: "+doc.id+" in organization: "+doc.ref.parent.parent.id);
    });
});

这将返回整个Firestore数据库中所有
人员
集合的快照。

这确实可行。但是,我的函数不应该在创建文档时触发,而应该在用户首次登录应用程序时触发。有没有办法修改您的代码,使其在第一次登录时触发?没有,据我所知,没有数据库,您无法触发该函数。