Firebase获取文档更新时的数据库值

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

更新某些值后,我想在数据库中查找特定值。但是,当我执行firebase deploy时,会得到以下信息:

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 ]

这是我的功能代码:

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

如果删除此选项,它会很好:

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

问题是什么,我如何使它起作用?

typescript firebase google-cloud-firestore google-cloud-functions
1个回答
3
投票

如果我这样添加catch(),则可以使用:

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

有趣的是,根据Firebase文档所说,不需要catch()

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