如何在字段值更新时触发Firestore云功能?

时间:2019-09-06 08:14:21

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

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

我的案子: 我有一个users(collections),其中有更多的userId(documents)文档,每个文档都包含字段和值。 字段之一是状态:true |错误(布尔值)。

如果要使用云功能和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) )

    } 

  });

3 个答案:

答案 0 :(得分:1)

由于在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(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) )
    } 
  });

答案 1 :(得分:1)

除“ 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

答案 2 :(得分:0)

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
}