【问题标题】:How to trigger firestore cloud functions on update of field value?如何在更新字段值时触发 Firestore 云功能?
【发布时间】:2020-01-09 02:31:05
【问题描述】:

我想使用 firestore 和云功能将用户的状态字段更新为值 (true) 时向用户发送电子邮件。

我的情况: 我有一个用户(集合),其中包含更多用户 ID(文档)文档,每个文档都包含字段和值。 其中一个字段是 status: true | false(布尔值)。

如果该用户的状态使用云功能和 sendgrid api 更改为 true,我想向该用户发送电子邮件。

users
   - dOpjjsjssdsk2121j131
        - id : dOpjjsjssdsk2121j131
        - status : false
   - pspjjsjssdsdsk2121j131
        - id : pspjjsjssdsdsk2121j131
        - status : false
   - yspjjsjssdsdsk2121j131
        - id : yspjjsjssdsdsk2121j131
        - status : false


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const SENDGRID_API_KEY = functions.config().sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.sendEmail = functions.firestore.document('users/{userId}')
  .onUpdate((change, context) => {  
   const after = change.after.val();

    if(after.status === 'VERIFIED'){
      console.log('profile verified')
      const db = admin.firestore();

      return db.collection('users').doc(userId)
      .get()
      .then(doc => {
         const user = doc.data();
         const msg = {
           to: 'email',
           from: 'email',
           templateId: 'template id',
           dynamic_template_data: {
            subject: 'Profile verified',
            name: 'name',
        },
       };

       return sgMail.send(msg)
   })
   .then(() => console.log('email sent!') )
   .catch(err => console.log(err) )

    } 

  });

【问题讨论】:

  • 您能否提供有关您的代码到底是什么不工作的信息

标签: android google-cloud-firestore google-cloud-functions


【解决方案1】:
you can compare the target field by before and after data, if your target field is not the same. It means the field value has changed.

    const beforedata=change.before.data()
const afterdata=change.after.data()

if(afterdata.status == beforedata.status){
// Status not changed
}else{
//status changed
}

【讨论】:

    【解决方案2】:

    您收到错误是因为在 Firestore 中没有 val()

    尝试将您的change.after.val() 更改为change.after.data(),因此您的代码如下所示:

    exports.sendEmail = functions.firestore.document('users/{userId}')
      .onUpdate((change, context) => {  
       const after = change.after.data();
    
        if(after.status === 'VERIFIED'){
          console.log('profile verified')
          const db = admin.firestore();
    
    
          return db.collection('users').doc(context.params.userId)  // get userId
          .get()
          .then(doc => {
             const user = doc.data();
             const msg = {
               to: 'email',
               from: 'email',
               templateId: 'template id',
               dynamic_template_data: {
                subject: 'Profile verified',
                name: 'name',
            },
           };
           return sgMail.send(msg)
       })
       .then(() => console.log('email sent!') )
       .catch(err => console.log(err) )
        } 
      });
    

    【讨论】:

    • 如何在 return.db.collection('users').doc(userId) 之前获取 userId。 ?
    • 我认为您必须使用 change.after.id 来获取 userId 或 after.id 因为您还将 id 存储在您的数据库中。
    【解决方案3】:

    除“context.params”外,上述答案均正确。 (staffId)

    中缺少

    目前:

    return db.collection('users').doc(userId)  // get userId
    

    应该是:

    >     return db.collection('users').doc(context.params.userId)  // get userId
    

    这也将回答上述评论:

    how to get the userId before return.db.collection('users').doc(userId) . ? – Fortray Sep 6 '19 at 9:13
    

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 2020-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2019-04-14
      • 2020-02-08
      • 1970-01-01
      相关资源
      最近更新 更多