doc.data();来自 firebase returns 未定义但打印正常?

doc.data(); from firebase returns undefined but prints normally?

所以我需要从 firebase 集合中获取具有给定 ID 的数据。然后我的函数应该 return 它(文档)并且在我的测试中它应该打印(如 'result')。出于某种原因,我的测试打印 'undefined' 但我的函数 (getIteration(id)) 准确打印出我需要的 return(在 return 上方的 console.log) .为什么 returning 未定义但使用相同的 doc.data() 打印出我需要的内容?

这是我的代码:

//gets the iteration with the given id
async function getIteration(id) {
    fb.db.collection('iteration').where('id', '==', id).get().then(snapshot => {
        snapshot.forEach(doc => {
            console.log(doc.data())
            return doc.data();
        })
    })
}

和我的测试:

firebase.getIteration(fbIterationID).then(function(result){
 console.log('LOGGING FB ITERATION DOCUMENT WITH THE ID OF: ' + fbIterationID)
    console.log(result)
})

您需要 return 来自 getIteration 的承诺,以便调用者可以使用其结果。使函数异步不会为你做那件事:

async function getIteration(id) {
    return fb.db...
}
    //gets the iteration with the given id
async function getIteration(id) {
    return fb.db.collection('iteration').where('id', '==', id).get().then(snapshot => {
        return snapshot.docs.map(doc => doc.data());
    })
}
import React, {useEffect, useLayoutEffect, useState} from 'react'
import {View, Text, StyleSheet, SafeAreaView, ScrollView, TouchableOpacity } from 'react-native'
import { Avatar } from 'react-native-elements/dist/avatar/Avatar';
import CustomListItem from '../../components/CustomListItem'
import { auth, db } from '../../services/firebase';
import {SimpleLineIcons} from '@expo/vector-icons'


export default function HomeScreen({ navigation }) {

    const [chats, setChats] = useState([]);
    const [userInfo, setUserInfo] = useState([])
    const [isDms, setIsDms] = useState(true);
    
  

    

    useEffect(() => {
        function unsubscribeDms(){ 
            db.collection('chats').onSnapshot(snapshot => {
            snapshot.docs.map(doc => {
                if (doc.data().isDM == true){
                    if (doc.data().users[0] == auth.currentUser.uid){
                        setChats(chats => [...chats, {id: doc.id, data: doc.data(), otherUser: doc.data().users[1]}])

                        db.collection("users").doc(doc.data().users[1]).get().then((doc) => {
                            setUserInfo(userInfo => [...userInfo, {uid: doc.id,  data: doc.data()}]
                                )
                        
                            
                        })
                        
                    }

                    else if (doc.data().users[1] == auth.currentUser.uid){
                        
                        setChats(chats => [...chats, {id: doc.id, data: doc.data(), otherUser: doc.data().users[0]}])

                        db.collection("users").doc(doc.data().users[0]).get().then((doc) => {
                            setUserInfo(userInfo => [...userInfo, {uid: doc.id,  data: doc.data()}]
                            )
                        
                            
                        })
                    }
                }

            })


            
        })}


        async function unsubscribeGcs(){
            await db.collection('chats').onSnapshot(snapshot => {
                snapshot.docs.forEach(doc => {
                    if (doc.data().isDM == false){
                        var allUsers  = doc.data().users
                        if (allUsers.includes(auth.currentUser.uid)){
                            setChats(chats => [...chats, {id: doc.id, data: doc.data()}])

                        }
                    }
                })
            })
        }

        if (isDms){
            unsubscribeDms()

        }

        else {
            unsubscribeGcs()
        }

        

        

    }, [])

    function getphotoUrl(uid){

        //return JSON.stringify(userInfo[0])


        return userInfo.length


        if (userInfo.length > 0){
            var picInfo = userInfo.filter((value) =>{
                return value.uid == uid
            })[0].uid
            //})[0].data.photoURL

            
    
    
    
            
         
            return picInfo

        }
        else {
            return 'null'
        }
    }

    function getDisplayName(uid){

        //return JSON.stringify(userInfo[0])

        
        return userInfo.length
     
    
        if (userInfo.length > 0){
            var nameInfo = userInfo.filter((value) =>{
                return value.uid == uid
            })[0].uid
            //})[0].data.displayName


            console.log("this is the display name" + nameInfo)




            return nameInfo
        }
        else {
            return 'null'
        }


    }
    
    

    function signOutUser(){
        auth.signOut().then(() => {
            navigation.replace('Login')
        })
    }


    


    useLayoutEffect(() => {
        navigation.setOptions({
            title: "Chats",          

            

            headerRight: () => (
                <View style={{
                    flexDirection: "row",
                    justifyContent: "space-between",
                    width: 80,
                    marginRight: 20

                }}>
                    
                <View style={{marginLeft: 20}}>
                    <TouchableOpacity onPress={() => navigation.navigate("Profile", {currentUser: auth.currentUser})} activeOpacity={0.5}>
                        <Avatar rounded source={{ uri: auth?.currentUser?.photoURL }}/>
                    </TouchableOpacity>
                </View>

                    <TouchableOpacity onPress={() => navigation.navigate("AddChat")}>
                        <SimpleLineIcons name="pencil" size={24} color="black"></SimpleLineIcons>
                    </TouchableOpacity>

                </View>
            )

            


        });
    }, [navigation])

    function enterChat(id, chatName, photo){
        navigation.navigate("Chat", {
            id: id,
            chatName: chatName,
            photo: photo

        })
    }

    return (
        <SafeAreaView>
           
            <ScrollView style={styles.container}>
                {chats.map(({id, data, otherUser}) => (
                    <CustomListItem key={id} id={id} enterChat={enterChat} photo={getphotoUrl(otherUser)} userName={getDisplayName(otherUser)} isDm={data.isDM}/>
                ))}
            </ScrollView>
        </SafeAreaView>
    )
}

const styles = StyleSheet.create({
    container: {
        height: "100%"
    }

})