无法使用数据库触发器,无法使用firebase云功能更新firebase数据库

时间:2018-05-27 20:34:39

标签: node.js typescript firebase firebase-realtime-database google-cloud-functions

我创建了一个firebase函数,用于在进行API调用时更新firebase数据库中的值。它给出了以下错误。

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /home/sumedhe/Project/mobile-atm-server/functions
> tslint --project tsconfig.json


ERROR: /home/sumedhe/Project/mobile-atm-server/functions/src/index.ts[12, 5]: Promises must be handled appropriately

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/sumedhe/.npm/_logs/2018-05-27T20_11_58_855Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

代码

import * as express from "express";
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
const app = express();
admin.initializeApp();

app.get("/mytransactions/", async function getUser(req: express.Request, res: express.Response) {

    // HERE IT GIVES THE ERROR //
    admin.database().ref('/modified').set("yes");
});
exports.api = functions.https.onRequest(app);

但是如果我使用与firebase数据库触发器相同的代码,它就可以正常工作。

exports.touch = functions.database.ref('/transactions/{item}').onWrite(
   (change, context) => admin.database().ref('/modified').set("yes"));

1 个答案:

答案 0 :(得分:2)

lint错误说:

[12, 5]: Promises must be handled appropriately

这意味着在第12行(从TypeScript转换的JavaScript)中,您需要注意处理承诺。以下函数返回一个promise:

admin.database().ref('/modified').set("yes");

你需要对这个承诺做点什么。鉴于这是一个HTTP类型函数,您应该根据promise的结果向客户端发送一些内容。也许是这样的:

admin.database().ref('/modified').set("yes")
.then(() => {
    res.send('ok')
})
.catch(err => {
    res.status(500).send(err)
})

由您决定要做什么,但您需要处理返回承诺的方法中的延续和错误,例如DatabaseReference.set()