Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected catch() or return (promise/catch-or-return)

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;
      });
    }
  });
});
like image 708
Shweta Chauhan Avatar asked Apr 28 '18 14:04

Shweta Chauhan


People also ask

What does catch return in promise?

This function has one argument: reason. The rejection reason. The Promise returned by catch() is rejected if onRejected throws an error or returns a Promise which is itself rejected; otherwise, it is fulfilled.

How promise catch works?

catch : when a promise fails, you catch the error, and do something with the error information. finally : when a promise settles (fails or passes), you can finally do something.

What is then catch in JavaScript?

The main difference between the forms promise. then(success, error) and promise. then(success). catch(error) is that in case if success callback returns a rejected promise, then only the second form is going to catch that rejection.

How do you get a promise rejection?

catch " around the executor automatically catches the error and turns it into rejected promise. This happens not only in the executor function, but in its handlers as well. If we throw inside a . then handler, that means a rejected promise, so the control jumps to the nearest error handler.


2 Answers

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')

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.

like image 197
31piy Avatar answered Sep 26 '22 14:09

31piy


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.

like image 45
e18r Avatar answered Sep 23 '22 14:09

e18r