1

I've got an async/await function call that authenticates a user in Firebase then I want it to take that newly created uid and create a new document of the same id, heres the code:

try{
    const currentUser = await auth.createUserWithEmailAndPassword(email, pass)
    await fdb.collection('users').doc(currentUser.user.uid).set({
        Forname: fname,
        Surname: sname,
        Liked: [],
    })
    dispatch({type:'USER_REG', payload: currentUser})
} catch(err){
    dispatch({type: 'ERROR', payload: 'Signin failed.'})
    console.error(err);
}

The first part works fine in that it adds a new user to authentication but the second async doesnt complete and neither does the dispatch within try.

I'm not getting any errors either so I'm not sure where I'm going wrong. Any help would be appreciated.

SlugLord
  • 143
  • 1
  • 9
  • Are you getting any error messages? Have you tried assigning your `await fdb.collection...` to a variable and logged it to the console? – Angus Ryer Oct 04 '21 at 20:29
  • I'm not getting any errors. But I also haven't tried you're suggestion. Thanks for the idea I'll give it a go and let you know. – SlugLord Oct 04 '21 at 20:49
  • @AngusRyer so with await prefixed console.log returns nothing, when I remove that it shows a pending Promise so its running but not resolving. The lack of any errors is a real pain XD. – SlugLord Oct 04 '21 at 20:56
  • Sorry for the formatting here: just to be sure, try this: `const result = await fdb.collection('users').doc(currentUser.user.uid).set({ Forname: fname, Surname: sname, Liked: [] }); console.log(result);` ---- and yes, I hear you, working with promises can be fun ;) – Angus Ryer Oct 04 '21 at 21:04
  • If the `set()` call is successful, the promise should resolve to `void` and console should log `undefined`, otherwise you'll get an error code. That's what you're getting? – Angus Ryer Oct 04 '21 at 21:07
  • @AngusRyer it looks like everything's running, however the doc creation is running very very slowly, I keep seeing this warning in chrome "[Violation] 'setInterval' handler took 359ms" (with the ms changing). No errors present and nothing from the console log is showing either. I think this may be an issue with Firebase. – SlugLord Oct 05 '21 at 11:02
  • That could be. [I found this reference](https://stackoverflow.com/a/67089233/7919105) to a way to suppress that warning, but that won't help you per se. You could try playing around with the `.set()` options, such as `set( ... , { merge: true } )` to see if that speeds things up, otherwise I'm not sure. Time for another SO question! – Angus Ryer Oct 05 '21 at 14:45
  • 1
    I think you're right, thanks for all the help @AngusRyer. – SlugLord Oct 05 '21 at 20:38

0 Answers0