0
votes

I've setup a simple onWrite listener on documents in my sandbox collection, just to test the listener API and see that I know how to use it.

Apparently I don't, because the following index.js in my functions dir:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sandbox = functions.firestore.document('sandbox/{wildcard}')
    .onWrite((change, context) => {
        console.log("In")
        console.log("Before is " + change.before)
        console.log("Wildcard is " + context.params.wildcard)
        return null
    })

results in the following output (whenever I add / remove a field in my sandbox/yoohoo document):

In
Before is undefined
TypeError: Cannot read property 'params' of undefined

What am I doing wrong?

1
did you update the firebase-function to v1? You can check the in package.json or try the old way onWrite(event)=>{ console.log(event.data.val())Peter Haddad
Thanks, I've got "firebase-functions": "^0.9.0" in my dependencies. Should I change it to "firebase-functions": "^1.0"?Dori
yes check the answer belowPeter Haddad

1 Answers

3
votes

It seems that you are using the older version of firebase-function, that is why params and change.after are undefined, to update you need to go to the functions folder, and do the following:

npm install firebase-functions@latest --save
npm install firebase-admin@latest --save

Also update firebase-tools:

npm install -g firebase-tools

then you will be able to use the new version, more info here:

https://firebase.google.com/docs/functions/beta-v1-diff