admin管理员组文章数量:1200965
I am new to JavaScript.This is my first function in javascript to deploy a function on the firebase.
Got this error:
- [eslint] Unexpected function expression. (prefer-arrow-callback)
- [eslint] Expected catch() or return (promise/catch-or-return)
What is wrong with this function?
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;
});
}
});
});
I am new to JavaScript.This is my first function in javascript to deploy a function on the firebase.
Got this error:
- [eslint] Unexpected function expression. (prefer-arrow-callback)
- [eslint] Expected catch() or return (promise/catch-or-return)
What is wrong with this function?
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;
});
}
});
});
Share
Improve this question
edited Apr 28, 2018 at 16:42
Doug Stevenson
317k36 gold badges454 silver badges472 bronze badges
asked Apr 28, 2018 at 14:37
Shweta ChauhanShweta Chauhan
6,9816 gold badges41 silver badges59 bronze badges
2
- Those are not errors, they are warnings from the linter. – Bergi Commented Apr 28, 2018 at 14:41
- You can find explanations of the rules at eslint.org/docs/rules – Bergi Commented Apr 28, 2018 at 14:43
2 Answers
Reset to default 20The 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')
or handle the error like this:
admin.database().ref('users/${uid}/referred_by').once('value')
// ... your code here
.catch(error => { ... });
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.
In order to solve promise/catch-or-return
, just add a .catch(() => null)
at the end of the line. And of course, don't forget to actually implement the error handling logic at some point in the not-so-distant future.
本文标签: javascriptExpected catch() or return (promisecatchorreturn)Stack Overflow
版权声明:本文标题:javascript - Expected catch() or return (promisecatch-or-return) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738621854a2103236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论