0

I can't figure out why the await function setDoc is not completed in my example below when I launch my react native app for the first time.

When I launch it a second time however, it works well.

Can you help me?

useEffect(() => {

    registerForPushNotificationsAsync().then(async token => {
      
      // The following gets called
      console.log("Before await")

      // The following does not complete when I first launch the app.
      await setDoc(doc(db, "devices", token), { test: "test" })
        .then(x => {
          // The following does not get called
          console.log('Sucess')
          
        })
        .catch(error => {
          // The following does not get called
          console.log('Error')
        })

      // The following does not get called
      console.log("After await")
    });

    return () => {};
  }, []);

with registerForPushNotificationsAsync defined outside useEffect as:

async function registerForPushNotificationsAsync() {
  ...
  return token;
}

Thank you.

5
  • Eithere use async-await or .then Commented Jul 20, 2022 at 7:42
  • @NullPointerException, I've just tried: removing .then() and .error() does not solve my problem. Commented Jul 20, 2022 at 7:51
  • Can you show your setDoc and doc methods? Commented Jul 20, 2022 at 7:59
  • These are the setDoc and doc methods from firebase: firebase.google.com/docs/firestore/manage-data/… Commented Jul 20, 2022 at 8:00
  • Might need to see your code in the registerForPushNotificationsAsync because the code you have posted doesn't explain the behaviour you are seeing Commented Jul 20, 2022 at 8:19

3 Answers 3

1

Try moving the async function outside of the useEffect function:

const someAsyncFunc = async () => {
    console.log("Before await")
    try {
        const token = await registerForPushNotificationsAsync();
        await setDoc(doc(db, "devices", token), { test: "test" })
        console.log('Success')
    } catch (error) {
        /// do error handling
        console.log(error);
    }
    console.log("After await")
}

useEffect(() => {
  someAsyncFunc();
}, []);
Sign up to request clarification or add additional context in comments.

2 Comments

I've just tried your solution, unfortunately it does not change the problem, the setDoc function does not complete.
I think your setDoc function does not work properly. Can you try debugging it? I would recommend to call the setDoc function with a button and add debug logs. I hope this helps.
0

use async await as follows.

useEffect(() => {
  registerForPushNotificationsAsync().then(async (token) => {
    // The following gets called
    console.log('Before await');

    try {
      await setDoc(doc(db, 'devices', token), { test: 'test' });
      console.log('Sucess');
    } catch (error) {
      console.log('Error');
    }

    // The following does not get called
    console.log('After await');
  });

  return () => {};
}, []);

1 Comment

Similar to the previous answer : I've just tried your solution, unfortunately it does not change the problem, the setDoc function does not complete.
0

If there a reason why you want to use await there ?

Otherwise you should try to do this using only .then and syncronious code :

useEffect(() => {

    return registerForPushNotificationsAsync().then(async token => {
    
    // The following gets called
    console.log("Before await")

    // The following does not complete when I first launch the app.
    return setDoc(doc(db, "devices", token), { test: "test" })
        .then(x => {
        // The following does not get called
        console.log('Sucess')
        
        })
        .then(() => {
            // The following does not get called
            console.log("After await")
            return () => {};
        })
        .catch(error => {
        // The following does not get called
        console.log('Error')
        })
    });
}, []);

1 Comment

It does not work either, thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.