【问题标题】:Firebase Cloud Functions async functionFirebase Cloud Functions 异步函数
【发布时间】:2023-04-10 09:28:02
【问题描述】:

我需要一些关于 firebase 和 node.js 的异步功能的帮助

我的 index.js 中有这个函数

const funcBiglietti = require('./biglietti');
    //Region biglietti 
exports.getBiglietti = functions.https.onRequest((req, res) => {

    let userid = req.url.replace('/',''); 
    let utente = admin.database().ref("Utenti").child(userid).once("value");

    var userInfo = {};

    utente.then(snap =>{
        if(snap === undefined)
            return res.status(400).send('utente non trovato.');
        else
            return userInfo = JSON.stringify(snap);

    }).catch(err => {
        return res.status(500).send('errore:' + err);
    })

    let tickets = await funcBiglietti.getBiglietti(userInfo,userid,admin.database());
    return res.status(200).send(tickets);
});

在 biglietti.js 中我有这个功能:

///Restituisce tutti i biglietti di un utente
exports.getBiglietti = async function(Utente,IDUtente,database){

    console.log('userinfo' + Utente);
    const biglietti = database.ref("Biglietti").child(IDUtente).once("value");
    biglietti.then(snap =>{
        console.log(JSON.stringify(snap));
        return snap;

    }).catch(err => {
        return err;
    })
}

我需要 index.js 中的函数来等待 biglietti.js 中的结果,但是当我尝试使用 async / await 时,我不断得到:

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

> functions@ lint /Users/Giulio_Serra/Documents/Server Firebase/Hangover/functions
> eslint .


/Users/Giulio_Serra/Documents/Server Firebase/Hangover/functions/biglietti.js
  3:30  error  Parsing error: Unexpected token function

/Users/Giulio_Serra/Documents/Server Firebase/Hangover/functions/locali.js
  13:9  warning  Avoid nesting promises  promise/no-nesting

✖ 2 problems (1 error, 1 warning)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
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.

我正在运行 node v 11.10

node -v
v11.10.0
MBP-di-Giulio:~ Giulio_Serra$ 

这是我的 package.json:

{
  "engines": {"node": "8"},
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "async": "^2.6.2",
    "firebase-admin": "~5.12.1",
    "firebase-functions": "^2.2.0",
    "request": "^2.88.0"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "private": true
}

使用异步函数我缺少什么?我有点迷路了。

编辑

通过更改代码解决:

//Region biglietti 
exports.getBiglietti = functions.https.onRequest((req, res) => {

    let userid = req.url.replace('/',''); 
    let utente = admin.database().ref("Utenti").child(userid).once("value");

    utente.then(snap =>{
        if(snap === undefined)
            return res.status(400).send('utente non trovato.');
        else{
            return funcBiglietti.getBiglietti(snap,userid,admin.database()).then(function(data){
                return res.status(200).send(data);
            }).catch(err => {
                return res.status(500).send('errore:' + err);
            })
        }    

    }).catch(err => {
        return res.status(500).send('errore:' + err);
    })
});

【问题讨论】:

    标签: javascript node.js firebase google-cloud-functions


    【解决方案1】:

    我不确定它是否适用于 Javascript,但请尝试将您的函数签名更改为:

    export async function getBiglietti(Utente,IDUtente,database){
    

    如果不行,可以试试箭头函数:

    exports.getBiglietti = async (Utente,IDUtente,database) => {
    

    您是否考虑过在您的函数中使用 Typescript?我发现它更安全、更易于维护,并且还具有一些开箱即用的最新语言功能。

    【讨论】:

    • 谢谢,我试试;)
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2019-10-07
    • 2018-08-13
    • 2022-07-28
    • 1970-01-01
    • 2018-06-11
    • 2018-01-17
    • 2017-11-28
    相关资源
    最近更新 更多