TypeError:change.after.val 不是一个函数 firebase

问题描述 投票:0回答:1

您好,我正在尝试读取我的 firestore 数据库中的更改

'use stricts'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
require('dotenv').config();

admin.initializeApp(functions.config().firebase);

exports.sendOrderCompletionEmail = functions.firestore.document('/service-requests/{req_id}')
    .onUpdate((change, context) => {
        const req = context.params.req_id;
            
        const sts = change.after.val();  //Error on this line
                
        console.log(sts);
                
    });

我收到此错误

TypeError: change.after.val is not a function

我想读取文档中的更改并返回更改的字段值

javascript firebase google-cloud-platform google-cloud-firestore google-cloud-functions
1个回答
5
投票

由于您是通过更新 Firestore 文档来触发云功能,因此您需要执行以下操作

const sts = change.after.data();

请参阅doc了解更多详细信息。


事实上,我们对由

Realtime Database
事件触发的 Cloud Functions 使用 val() 方法。

Firebase 提供两种 NoSQL 数据库解决方案:Cloud Firestore 和实时数据库。请参阅此doc了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.