我需要有关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);
})
});
答案 0 :(得分:0)
我不确定它是否可以与Javascript一起使用,但是请尝试将函数签名更改为此:
export async function getBiglietti(Utente,IDUtente,database){
如果没有,您可以尝试使用箭头功能:
exports.getBiglietti = async (Utente,IDUtente,database) => {
您是否考虑过在函数上使用Typescript?我发现它更安全,更容易维护,并且还具有一些现成的语言功能。