27

I'm building an application with Firebase oAuth. I followed all the instructions, but my code is returning an error saying that 'asyn arrow function expected no return value'.

I saw that there are multiple posts with the same title, but have not found an answer.

enter image description here

import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';

export const auth = firebase.auth();
export const firestore = firebase.firestore();

const config = {
  apiKey: 'XXXXXXXXX',
  authDomain: 'XXXXXXXXX',
  projectId: 'XXXXXXXXX',
  storageBucket: 'XXXXXXXXX',
  messagingSenderId: 'XXXXXXXXX',
  appId: 'XXXXXXXXX',
};
firebase.initializeApp(config);

export const createUserProfileDocument = async (userAuth, additionalData) => {
  if (!userAuth) return;

  const userRef = firestore.doc(`users/${userAuth.uid}`);

  const snapShot = await userRef.get();

  if (!snapShot.exists) {
    const { displayName, email } = userAuth;
    const createdAt = new Date();
    try {
      await userRef.set({
        displayName,
        email,
        createdAt,
        ...additionalData,
      });
    } catch (error) {
      console.log('error creating user', error.message);
    }
  }

  return userRef;
};


const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({ prompt: 'select_account' });
export const signInWithGoogle = () => auth.signInWithPopup(provider);

export default firebase;
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
Augusto Mallmann
  • 461
  • 1
  • 5
  • 10

6 Answers6

41

Change the first line from if (!userAuth) return to if (!userAuth) return {}, this should do the trick

Explanation
The error you're having says consistent-return at the end, which basically means your function should consistently return the same type.

With that in mind, if we take a look at the first line in your function

if (!userAuth) return;

this line returns a void, but the last line in the function is returning some other type

return userRef;

userRef is definitely not of type void and this is why you are having this error.

akram-adel
  • 920
  • 7
  • 8
7

The error message is telling you that an arrow function should always or never return a value. Look here

You have if (!userAuth) return; and return userRef;

Change if (!userAuth) return {}; should fix the problem.

FortyTwo
  • 2,414
  • 3
  • 22
  • 33
5

This rule requires return statements to either always or never specify values.

Examples of INCORRECT code for this rule:

function doSomething(condition) {
    if (condition) {
        return true;
    } else {
        return;
    }
}

function doSomething(condition) {
    if (condition) {
        return true;
    }
}

Examples of CORRECT code for this rule:

function doSomething(condition) {
    if (condition) {
        return true;
    } else {
        return false;
    }
}

// or even better:
function doSomething(condition) {
    return condition;
}

Read more

If you want to disable this rule you can use this comment:

// eslint-disable-next-line consistent-return
return userRef;
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
3

try to replace return to return undefined

  • For most of the cases returning null instead of undefined is better. There is even a eslint rule about that. Check: https://stackoverflow.com/questions/37980559/is-it-better-to-return-undefined-or-null-from-a-javascript-function – Mansur Feb 08 '22 at 12:41
1

There is a violation of the eslint 'consistent-return' rule in the code.

This code:

if (!userAuth) return;

returns undefined, while the last line of the arrow function:

  return userRef;
};

returns userRef.

The fix is to return something that isn't undefined in the first case.

terrymorse
  • 6,771
  • 1
  • 21
  • 27
0

You can replace

if (!userAuth) return;

with

if (!userAuth) return undefined;
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
jsLuc
  • 16
  • 1