简体   繁体   English

undefined 不是 object on react-native expo with firestore

[英]undefined is not an object on react-native expo with firestore

I would like to have some help with my codes.我想在我的代码方面得到一些帮助。 I am currently developing a quiz application.我目前正在开发一个测验应用程序。 I need to get my questions and answers from my firestore data base.我需要从我的 firestore 数据库中获取我的问题和答案。 but when I am retrieving data I always get the error undefined is not an object(evaluating '_userDoc.incorrect_answer') help me.但是当我检索数据时,我总是得到错误 undefined is not an object(evaluating '_userDoc.incorrect_answer') 帮助我。

this is my code这是我的代码

import { StyleSheet, Text, TouchableOpacity, View, SafeAreaView, ScrollView, FlatList, Image, Alert, BackHandler } from 'react-native'
import { StatusBar } from 'expo-status-bar'
import React, { Component, useEffect, useState } from 'react'
import { doc, setDoc, Timestamp, getDoc } from "firebase/firestore"; 
import { auth,db } from '../config';

const shuffleArray=(array)=> {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
  }

const Quiz  = ({navigation}) => {
    const [isActive, setIsActive] = useState(false)
    const[userDoc, setUserDoc] = useState();
    const [options, setOptions] = useState([]);
    const [loading, setLoading] = useState(false);

   let docu = "question"+2;
    const myDocs = doc(db, "subject", docu)
   
useEffect(()=>{
    getQuiz();
    },[])

const getQuiz = async()=>{
    setLoading(true);
    getDoc(myDocs)
    .then((snapshot) =>{
        if(snapshot.exists){
            setUserDoc(snapshot.data())
            setOptions(generateOptionsAndShuffle(userDoc))
        }else{
            alert("No Doc FOUND")
        }
    })
    .catch((error)=>{
    alert(error.message)
    })
}
const generateOptionsAndShuffle=(_userDoc)=>{
    let options = [..._userDoc.incorrect_answer];
    options.push(_userDoc.correct_answer);
    shuffleArray(options)
    console.log(options)
    return options
  }
  
    const thisHasPress=()=>{
        setIsActive(current => !current);
    }

    let energy = 5;
    let score =0;
    
    return (
        <SafeAreaView style={styles.container}> 
        <StatusBar style="dark" />
       <View style={styles.headerContainer}>
           <Text style={styles.headerText}>THIS IS QUIZ</Text>
       </View>
       
       <View style={styles.questionContainer}>
           <Text style={styles.questionText}>{userDoc?.question || ''}</Text>
       </View>      
           <View style={styles.buttonChoicesContainer}>
           
               <TouchableOpacity style={ isActive ? styles.buttonChoicesSelected : styles.buttonChoices}>
                <Text style={isActive ? styles.buttonChoicesTextSelected : styles.buttonChoicesText}>{options[0] || ''}</Text>
               </TouchableOpacity >
               <TouchableOpacity style={ isActive ? styles.buttonChoicesSelected : styles.buttonChoices} onPress={thisHasPress} >
                   <Text style={isActive ? styles.buttonChoicesTextSelected : styles.buttonChoicesText}>{options[1] || ''}</Text>
               </TouchableOpacity>
               <TouchableOpacity style={ isActive ? styles.buttonChoicesSelected : styles.buttonChoices} onPress={thisHasPress} >
                   <Text style={isActive ? styles.buttonChoicesTextSelected : styles.buttonChoicesText}>{options[2] || ''}</Text>
               </TouchableOpacity>
               <TouchableOpacity style={ isActive ? styles.buttonChoicesSelected : styles.buttonChoices} onPress={thisHasPress} >
                   <Text style={isActive ? styles.buttonChoicesTextSelected : styles.buttonChoicesText}>{options[3] || ''}</Text>
               </TouchableOpacity>
           </View>  
       <View style={styles.footer}>
           <TouchableOpacity style={styles.footerButton} onPress={()=>{ navigation.navigate("Dashboard") }}>
               <Image source={require('../assets/home-dark.png')} 
                           style={styles.navIcons}
                           resizeMode="contain"/>
           </TouchableOpacity>
           <TouchableOpacity style={styles.footerButtonReversed} onPress={()=>{ if(energy!=0){energy=energy-5};score++; navigation.navigate("Result",{ energy: energy, score: score}); console.log(energy) }}>
            <Text>SUBMIT</Text>
           </TouchableOpacity>
           
       </View>
   </SafeAreaView>
    )
  }


export default Quiz

I already tried searching for many solutions but it isn't working.我已经尝试搜索许多解决方案,但没有用。

You may try replacing this line in your getQuiz function您可以尝试在 getQuiz function 中替换此行

setOptions(generateOptionsAndShuffle(snapshot.data()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何将 firestore object 转换成我可以在 expo/react-native 中使用的东西? - How should I convert a firestore object into something I can use in expo/react-native? Firestore 查询在 react-native 中不起作用 - A firestore query not working in react-native Expo 项目类型错误:未定义不是 object(评估“_firebase.default.firestore”) - Expo project TypeError: undefined is not an object (evaluating '_firebase.default.firestore') 放大错误:[TypeError: undefined is not an object (evaluating '_awsAmplify.default.configure')] in a React Native, Expo app - Amplify error: [TypeError: undefined is not an object (evaluating '_awsAmplify.default.configure')] in a React Native, Expo app react-native firestore:使用不同字段使用 where() 和 orderBy() 进行查询 - react-native firestore: Query with where() and orderBy() using different fields 无法将 Firestore 文档数据加载到 React-Native 中的变量 - Can't load firestore document data to variable in React-Native React Native Expo Firebase Firestore (9.9.3):无法访问 Cloud Firestore 后端 - React Native Expo Firebase Firestore (9.9.3): Could not reach Cloud Firestore backend 在 React Native 中检索 Firestore Object 数组值 - Retrieving Firestore Object Array Values in React Native 如何使用 react-native 和 expo 将 m4a 音频文件上传到 Firebase 存储? - How does one upload an m4a audio file to Firebase Storage using react-native and expo? 我应该安装哪个版本的node,react-native、expo、firebase等,才不会出现依赖问题? - Which version should I install node, react-native, expo, firebase, etc. so that there are no dependency problems?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM