【问题标题】:Firebase get database values on document updateFirebase 在文档更新时获取数据库值
【发布时间】:2020-03-13 19:53:25
【问题描述】:

当某些值更新时,我想在我的数据库中查找特定值。但是,当我做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);
})

有什么问题以及如何解决?

【问题讨论】:

  • 请编辑问题以显示 Firebase CLI 的整个输出,以及您不了解的具体内容。它可能会告诉你出了什么问题。
  • 我不明白应该如何解决我的问题

标签: typescript firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

如果我像这样添加catch(),它会起作用:

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

有趣的是,根据 Firebase 文档,catch() 不是必需的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多