预期的catch()或返回(承诺/捕获或返回) [英] Expected catch() or return (promise/catch-or-return)
问题描述
我是JavaScript的新手.这是我在javascript中将第一个功能部署在firebase上的第一个功能.
I am new to JavaScript.This is my first function in javascript to deploy a function on the firebase.
收到此错误:
- [eslint] Unexpected function expression. (prefer-arrow-callback)
- [eslint] Expected catch() or return (promise/catch-or-return)
此功能有什么问题?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.grantSignupReward = functions.database.ref('/users/{uid}/last_signin_at')
.onCreate(event => {
var uid = event.params.uid;
admin.database().ref('users/${uid}/referred_by')
.once('value').then(function(data) {
var referred_by_somebody = data.val();
if (referred_by_somebody) {
var moneyRef = admin.database()
.ref('/users/${uid}/inventory/pieces_of_eight');
moneyRef.transaction(function(current_value) {
return (current_value || 0) + 50;
});
}
});
});
推荐答案
第一个错误建议您使用箭头函数作为回调.因此,您需要将常规函数(function(){...})
替换为箭头函数((()=> {...})
The first error suggests you to use an arrow function as a callback. So you need to replace the regular functions, (function() { ... })
, with arrow functions, (() => { ... })
.
第二个错误表明您要么需要拒绝承诺,要么返回承诺本身.我对您的代码不太确定,但是我相信这种方法:
The second error suggests that you either need to catch the promise rejection, or return the promise itself. I am not too sure about your code but I believe that this method:
admin.database().ref('users/${uid}/referred_by').once('value')
兑现承诺.因此,需要像这样返回它:
returns a promise. So it needs to be returned like this:
return admin.database().ref('users/${uid}/referred_by').once('value')
或处理如下错误:
admin.database().ref('users/${uid}/referred_by').once('value')
// ... your code here
.catch(error => { ... });
正如@Bergi在评论中指出的那样,此处不希望返回承诺,您可以在承诺中添加 catch
块.
As @Bergi pointed out in the comments that returning the promise is not preferable here, you may just add a catch
block to your promise.
这篇关于预期的catch()或返回(承诺/捕获或返回)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!