Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async arrow function expected no return value

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;
like image 474
Augusto Mallmann Avatar asked Dec 26 '20 20:12

Augusto Mallmann


People also ask

Does async work with arrow function?

Async functions can also be defined with the arrow syntax.

What is consistent return?

This rule requires return statements to either always or never specify values. This rule ignores function definitions where the name begins with an uppercase letter, because constructors (when invoked with the new operator) return the instantiated object implicitly if they do not return another object explicitly.

Is return await redundant?

When to use return await. return await should only be used inside a try… catch as it is redundant otherwise. This can be enforced with the ESLint rule no-return-await .


1 Answers

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.

like image 75
akram-adel Avatar answered Sep 28 '22 08:09

akram-adel