0

I'm trying to access the data inside of my user object in my firestore databases. However, I keep getting promises instead of the objects.

function StorefrontPage(props) {
  const auth = useAuth();
  const router = useRouter();
  const [user, setUser] = useState({});

  const uid = auth.user && auth.user.uid;


  useEffect(() => { uid && getUser(uid, setUser);
  console.log(user);
  }, [])

  console.log(user);

The first time I run this, it returns the objects I want, but subsequent refreshes and page changes will turn the console.log into a proto object/promise. I thought wrapping it inside of the useEffect hook would solve this issue, but it is not. How do I make sure it always returns the user object instead of a promise?

For reference, the getUser function is:

export function getUser2(uid, setUser) {
  return firestore.collection('users')
                 .doc(uid).get()
                 .then(user => setUser(user))
                 .catch(err => console.log(err));        
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
bguan2020
  • 93
  • 1
  • 8
  • This is expected. `then` and `catch` both return promises the resolves when the entire chain of promises resolves. – Doug Stevenson Jun 24 '20 at 19:10
  • @DougStevenson how do i get it to return the object instead of promise – bguan2020 Jun 24 '20 at 19:18
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Roamer-1888 Jun 25 '20 at 00:45

0 Answers0