[英]Firebase Cloud Functions async function
I need some help with async function with firebase and node.js 我需要有关Firebase和node.js的异步功能的帮助
I have this function in my index.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);
});
instead in biglietti.js i have this function: 相反,在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;
})
}
I need the function in index.js to wait the result in biglietti.js but when I'm trying to use the async / await i keep getting: 我需要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.
I'm running node v 11.10 我正在运行节点v 11.10
node -v
v11.10.0
MBP-di-Giulio:~ Giulio_Serra$
and here is my package.json: 这是我的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
}
What I'm missing to use async functions? 我缺少使用异步功能的东西? I'm a little bit lost.
我有点迷路了。
EDIT 编辑
resolved by changing code like this: 通过更改如下代码来解决:
//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);
})
});
I'm not sure if it will work with Javascript, but try changing your function signature to this: 我不确定它是否可以与Javascript一起使用,但是尝试将函数签名更改为:
export async function getBiglietti(Utente,IDUtente,database){
If not, you can try an arrow function: 如果没有,您可以尝试使用箭头功能:
exports.getBiglietti = async (Utente,IDUtente,database) => {
Have you considered using Typescript on your functions? 您是否考虑过在函数上使用Typescript? I find it much safer and easier to maintain, and also has some recent language features out of the box.
我发现它更安全,更容易维护,并且还具有一些现成的语言功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.