小编For*_*ray的帖子

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

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

我的情况:我有一个用户(集合),它有更多的 userId(文档)文档,每个文档都包含字段和值。字段之一是 status: 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 …
Run Code Online (Sandbox Code Playgroud)

android google-cloud-functions google-cloud-firestore

3
推荐指数
1
解决办法
6425
查看次数