Vitaly Kalenik
Vitaly Kalenik

Reputation: 420

Firebase get database values on document update

I want to look for specific values in my database when some values are updated. However, when I do firebase deploy I get this:

12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `tslint --project tsconfig.json`
13 verbose stack Exit status 2
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:311:20)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:311:20)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid functions@
15 verbose cwd /Users/u17495358/firecast
16 verbose Darwin 18.7.0
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "--prefix" "/Users/u17495358/firecast/functions" "run" "lint"
18 verbose node v12.16.1
19 verbose npm  v6.13.4
20 error code ELIFECYCLE
21 error errno 2
22 error functions@ lint: `tslint --project tsconfig.json`
22 error Exit status 2
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 2, true ]

This is my function code:

import * as functions from 'firebase-functions';
import admin = require('firebase-admin');

admin.initializeApp()

export const onLocationUpdate = 
functions.firestore.document("users/{userId}").onUpdate(
    change => {
        let data = change.before.data();
        console.log(data);
        for (const key in data) {
            const value = data[key];
            console.log(value);
        }
        change.before.ref.get().then(document => {
            console.log(document.data);
        })
        let payload = {
            data: {
                temp: String(""),
                conditions: String("")
            }
        }
        return admin.messaging().sendToTopic("users", payload)
    }
)

It works well if I remove this:

change.before.ref.get().then(document => {
    console.log(document.data);
})

What is the problem and how I can make it work?

Upvotes: 0

Views: 46

Answers (1)

Vitaly Kalenik
Vitaly Kalenik

Reputation: 420

It works if I add catch() like this:

change.before.ref.get().then(document => {
            console.log(document.data);
        }).catch(error => console.log(error))

Interestingly according to the Firebase documentation where is said that catch() is not required.

Upvotes: 3

Related Questions