getting a document from firestore, in a method that returns undefined

Chris_Tophy GC

so, i'm trying to get a single document from a collection, using the id from the current user, the request [getDoc] works, but when i'm trying to return the information from the exported function is where i', having all the problems, the function is called but it acts before the function returns the values it needs, so it only gets undefined information, i've tryed with .then(), async functions but it doesn't work at all

this is on the main javascript file called by the html <•script type="module" src="./Home.js">

window.addEventListener('DOMContentLoaded', async() => {
    UserStatus()
    
    const userinfo = await getUserInfo('UniversityStaff');
    //.then((userinfo) => {
        console.log(userinfo.data())
    //     const userProfile = userinfo.data();
    //     let html ='';
    //     html+=`
    //     <h5><b>Usuario:</b>${userProfile.userName}</h5>
    //     <h5><b>Correo institucional:</b>${userProfile.email}</h5>
    //     <h5><b>Telefono:</b>${userProfile.phone}</h5>
    //     <h5><b>Departamento:</b>${userProfile.area}</h5>
    //     <h5><b>Puesto:</b>${userProfile.jobTitle}</h5>
    //     <h5><b>Horario:</b>${userProfile.workingHours}</h5>`
    
    //     userInfo.innerHTML = html;
    // })
    })

this is the javascript file that contains all the code from the firebase modules. this is imported by the ./Home.js

export const getUserInfo = (collection) => {
       onAuthStateChanged(auth, (user) => {
        if (user) {
          getDoc(doc(db, collection, user.uid))
          .then((doc)=>{
            console.log(doc.data())
            return doc;

          })
        }
      })
    }
•this is the error from the [.then()/await] getting done before the [export const getUserInfo]


Home.js:14 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data') at Home.js:14:30 (anonymous) @ Home.js:14



•this is the data from the request on [export const getUserInfo]


firebase.js:112 {area: '', id: 'jX3J5XjwDphlE1rQRxlxOHyDqMI2', email: '[email protected]', phone: '6181690397', userName: 'Edmundo Gurrola', …}

Frank van Puffelen

The top-level code in your getUserInfo function doesn't return any value, so the await you use when calling that function is meaningless - as is the return doc inside the getDoc callback.

Since you want to await the async operations, you'll need to return a promise get getUserInfo:

export const getUserInfo = (collection) => {
  return new Promise((resolve, reject) => {
    onAuthStateChanged(auth, (user) => {
      if (user) {
        getDoc(doc(db, collection, user.uid)).then((doc)=>{            
          resolve(doc)
        })
      }
    })
  })
}

So the Promise that this code returns only resolves once the document has been loaded, meaning your await gets the result it expects.

Also see:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting an ArrayList from Firestore and the document name

Getting the id from a just created Firestore document

Getting a Cloud Firestore document reference from a documentSnapshot

Populating a state after getting a document from the Firestore

Document not getting deleted in firestore

Firestore onSnapshot returns undefined

Getting a specific field in a document returned from firebase firestore

Getting an error when trying to retrieve data from Firebase FireStore document

Getting parent and child document data from Firestore in a single Vue component

Unable to set state after getting document data from Firestore

Getting array element from parsed json returns undefined

Getting a field value from Redux form returns 'undefined'

Getting a document synchronously using FireStore

Getting the documentID of a Firestore document in flutter?

method in constructor returns undefined

document.getElementByClassName returns undefined

Getting an undefined in object method

this refs is getting undefined in method

Document.get on nested document returns undefined

405 Method Not Allowed getting couchdb document from javascript

Document is not deleting from firestore

Angular - Calling method on service from component returns undefined

Getting Polyline from Firestore

Getting a Map from Firestore

Flutter :Cannot read properties of undefined (reading 'id') while accessing document id from cloud firestore

Firestore Document Typescript doc.data() undefined?

Firestore Document reference is always undefined in react native

Is it possible for Firestore document Snapshot data to be undefined?

Firestore returns a document even though none exists