TypeError: change.after.val is not a function firebase

Viewed 449

Hi there I am trying to read changes in my firestore database

'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);
                
    });

I am getting this error

TypeError: change.after.val is not a function

I want to read changes in my document and return the field value which is changed

1 Answers

Since you are triggering your Cloud Function by the update of a Firestore document, you need to do

const sts = change.after.data();

See the doc for more details.


As a matter of fact we use the val() method for Cloud Functions triggered by a Realtime Database event.

Firebase offers two NoSQL database solutions: Cloud Firestore and the Realtime Database. See this doc for more details.

Related