簡體   English   中英

Firebase Cloud Functions異步功能

[英]Firebase Cloud Functions async function

我需要有關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.

我正在運行節點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一起使用,但是嘗試將函數簽名更改為:

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

如果沒有,您可以嘗試使用箭頭功能:

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

您是否考慮過在函數上使用Typescript? 我發現它更安全,更容易維護,並且還具有一些現成的語言功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM