doc.data(); from firebase 返回未定义但打印正常?

因此,我需要从 firebase 集合中获取具有给定 id 的数据。然后我的函数应该返回它(文档),并且在我的测试中它应该打印(作为“结果”)。出于某种原因,我的测试打印了“未定义”,但我的函数 (getIteration(id)) 准确地打印出我需要它返回的内容(在返回上方的 console.log 中)。为什么它返回 undefined 但使用相同的 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)

})


qq_遁去的一_1
浏览 101回答 3
3回答

慕仙森

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

慕田峪9158850

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 }) {&nbsp; &nbsp; const [chats, setChats] = useState([]);&nbsp; &nbsp; const [userInfo, setUserInfo] = useState([])&nbsp; &nbsp; const [isDms, setIsDms] = useState(true);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; useEffect(() => {&nbsp; &nbsp; &nbsp; &nbsp; function unsubscribeDms(){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.collection('chats').onSnapshot(snapshot => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snapshot.docs.map(doc => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (doc.data().isDM == true){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (doc.data().users[0] == auth.currentUser.uid){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setChats(chats => [...chats, {id: doc.id, data: doc.data(), otherUser: doc.data().users[1]}])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.collection("users").doc(doc.data().users[1]).get().then((doc) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setUserInfo(userInfo => [...userInfo, {uid: doc.id,&nbsp; data: doc.data()}]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (doc.data().users[1] == auth.currentUser.uid){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setChats(chats => [...chats, {id: doc.id, data: doc.data(), otherUser: doc.data().users[0]}])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.collection("users").doc(doc.data().users[0]).get().then((doc) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setUserInfo(userInfo => [...userInfo, {uid: doc.id,&nbsp; data: doc.data()}]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; &nbsp; &nbsp; async function unsubscribeGcs(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await db.collection('chats').onSnapshot(snapshot => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; snapshot.docs.forEach(doc => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (doc.data().isDM == false){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var allUsers&nbsp; = doc.data().users&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (allUsers.includes(auth.currentUser.uid)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setChats(chats => [...chats, {id: doc.id, data: doc.data()}])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (isDms){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unsubscribeDms()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unsubscribeGcs()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }, [])&nbsp; &nbsp; function getphotoUrl(uid){&nbsp; &nbsp; &nbsp; &nbsp; //return JSON.stringify(userInfo[0])&nbsp; &nbsp; &nbsp; &nbsp; return userInfo.length&nbsp; &nbsp; &nbsp; &nbsp; if (userInfo.length > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var picInfo = userInfo.filter((value) =>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value.uid == uid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })[0].uid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //})[0].data.photoURL&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return picInfo&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'null'&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; function getDisplayName(uid){&nbsp; &nbsp; &nbsp; &nbsp; //return JSON.stringify(userInfo[0])&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return userInfo.length&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (userInfo.length > 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var nameInfo = userInfo.filter((value) =>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value.uid == uid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })[0].uid&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //})[0].data.displayName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("this is the display name" + nameInfo)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nameInfo&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'null'&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; function signOutUser(){&nbsp; &nbsp; &nbsp; &nbsp; auth.signOut().then(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; navigation.replace('Login')&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; useLayoutEffect(() => {&nbsp; &nbsp; &nbsp; &nbsp; navigation.setOptions({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: "Chats",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerRight: () => (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <View style={{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flexDirection: "row",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; justifyContent: "space-between",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 80,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; marginRight: 20&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <View style={{marginLeft: 20}}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TouchableOpacity onPress={() => navigation.navigate("Profile", {currentUser: auth.currentUser})} activeOpacity={0.5}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Avatar rounded source={{ uri: auth?.currentUser?.photoURL }}/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TouchableOpacity>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </View>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TouchableOpacity onPress={() => navigation.navigate("AddChat")}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SimpleLineIcons name="pencil" size={24} color="black"></SimpleLineIcons>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TouchableOpacity>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </View>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }, [navigation])&nbsp; &nbsp; function enterChat(id, chatName, photo){&nbsp; &nbsp; &nbsp; &nbsp; navigation.navigate("Chat", {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chatName: chatName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo: photo&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; <SafeAreaView>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ScrollView style={styles.container}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {chats.map(({id, data, otherUser}) => (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CustomListItem key={id} id={id} enterChat={enterChat} photo={getphotoUrl(otherUser)} userName={getDisplayName(otherUser)} isDm={data.isDM}/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ))}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ScrollView>&nbsp; &nbsp; &nbsp; &nbsp; </SafeAreaView>&nbsp; &nbsp; )}const styles = StyleSheet.create({&nbsp; &nbsp; container: {&nbsp; &nbsp; &nbsp; &nbsp; height: "100%"&nbsp; &nbsp; }})

斯蒂芬大帝

您需要从那里返回承诺,getIteration以便调用者可以使用其结果。使函数异步不会为您做到这一点:async function getIteration(id) {&nbsp; &nbsp; return fb.db...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript