I have an array in my firestore database that holds categories of services. When a user enters the details section I want to display these categories.
My problem is that whenever I enter the details Screen I get an error when I am trying to map the array.
i get an error here :
{details.category.map((category,index)=> (
<View style={styles.categoryContainer} key={index}>
<FontAwesome name="tag" size={16} color="#fff" />
<Text style={styles.category}>{category}</Text>
</View>
))}
"Render Error: undefined is not an object, evaluating details.category.map"
Even though I get all the correct details if I remove this line of code. What am i doing wrong?
here is my code:
const DetailsScreen = ({route}) => {
const [details, setDetails] = useState([]);
const servID = route.params.serviceID;
const navTitleView = useRef(null);
const fetchServicesDetails = async () => {
try{
firestore()
.collection("Services")
.doc(servID)
.get()
.then((snapshot) => {
if (snapshot.exists) {
setDetails(snapshot.data());
} else {
console.log("doesn't exist");
}
})
}catch(e){
console.log(e)
}
}
useEffect(()=>{
fetchServicesDetails();
},[]);
return (
<View style={styles.container}>
<View style={styles.section}>
<View style={styles.categories}>
{details.category.map((category,index)=> (
<View style={styles.categoryContainer} key={index}>
<FontAwesome name="tag" size={16} color="#fff" />
<Text style={styles.category}>{category}</Text>
</View>
))}
</View>
</View>
);
};
export default DetailsScreen;
