import { Pressable, Text, TextInput, View, Image, TouchableOpacity, Modal } from…
- User
import { Pressable, Text, TextInput, View, Image, TouchableOpacity, Modal } from ‘react-native’; import { gStyle } from ‘…/styles/style’; import { Feather } from ‘@expo/vector-icons’; import React, { useState, useEffect } from ‘react’; import { useNavigation } from ‘@react-navigation/native’; import { firebase } from ‘…/Firebase/firebase’; import ‘firebase/compat/auth’; import ‘firebase/compat/database’; import ‘firebase/compat/firestore’;
export default function Header() { const [searchTerm, setSearchTerm] = useState(‘’);
const [searchResult, setSearchResult]= useState(‘’);
const handleSearch = async ()=>{ if(!searchTerm.trim()){ setSearchResult([]); return; } const futureMKSnapshot = await firebase .firestore() .collection(‘FutureMK’) .where(‘name’,‘>=’,searchTerm) .where(‘name’,‘<=’,searchTerm + ‘\uf8ff’) .get();
const pastMKSnapshot = await firebase .firestore() .collection(‘PastMK’) .where(‘name’,‘>=’,searchTerm) .where(‘name’,‘<=’,searchTerm + ‘\uf8ff’) .get();
const futureMKData = futureMKSnapshot.docs.map(doc => ({ id: doc.id, …doc.data(), }));
const pastMKData = pastMKSnapshot.docs.map(doc => ({ id: doc.id, …doc.data(), }));
const results = […futureMKData, …pastMKData].filter(item=> item.name.toLowerCase().includes(searchTerm.toLowerCase())); const storage=firebase.storage; const resultWithImages = await Promise.all( results.map(async item=>{ try{ const imagePath=item.image; const imageRef=storage.ref().child(imagePath); const url = await imageRef.getDownloadURL(); return {… item, image:url}; }catch(error){ console.log(‘Ошибка загрузки изображения: ‘, error); return item; } }) ) setSearchResult(resultWithImages); navigation.navigate(‘Search’, { results: resultWithImages }); };
const [userAuthenticated, setUserAuthenticated] = useState(false);
const [isModalVisible, setIsModalVisible] = useState(false);
const showModal = () => { setIsModalVisible(true); };
const hideModal = () => { setIsModalVisible(false); };
const navigation = useNavigation();
useEffect(() => { const unsubscribe = firebase.auth().onAuthStateChanged(user => { if (user) { setUserAuthenticated(true); } else { setUserAuthenticated(false); } });
return unsubscribe; }, []); return ( <Image source={require(’…/assets/logo.png’)} style={gStyle.logoImg} /> Х <TouchableOpacity onPress={()=>navigation.navigate(‘Home’)}
Главная <TouchableOpacity onPress={()=>navigation.navigate(‘AllFutureMK’)}
Будущие мастер-классы <TouchableOpacity onPress={()=>navigation.navigate(‘AllPastMK’)}
Архив мастер-классов <TouchableOpacity onPress={()=>navigation.navigate(‘Sales’)}
Скидки {userAuthenticated ? <TouchableOpacity onPress={()=>navigation.navigate(‘Profile’)}
- Assistant
It seems like there might be an issue with the image path or with the way the images are being retrieved from Firebase Storage. Here are a few things you can try:
-
Make sure that the image path is correct and matches the path in Firebase Storage.
-
Check if the Firebase Storage rules allow public access to the images. If not, you might need to update the rules to allow access.
-
Verify that the images are being retrieved correctly by logging the URL returned by
getDownloadURL()in the console. You can also try to display the image directly using the URL to see if it loads correctly. -
Try using a different image format or optimizing the image size to see if that helps with the loading. Large images can take longer to load, especially on slow connections.
Hopefully, one of these solutions will help you resolve the issue and display the images correctly in your app.
-
- User
the path is correct and imgs are allowed to seen
- Assistant
If the image path is correct and the images are allowed to be seen in Firebase Storage, then the issue might be with the way you're handling the promise returned by
getDownloadURL().At the moment, you're using
Promise.all()to wait for all thegetDownloadURL()calls to complete before returning the result. However, if one of the promises fails, the entire operation will fail, and you won't be able to display any images.To debug this issue, try logging the error message in the catch block to see if there are any errors thrown by
getDownloadURL().Here's an example of how you can modify the code to handle errors more gracefully:
codeconst resultWithImages = results.map(async item => { try { const imagePath = item.image; const imageRef = firebase.storage().ref().child(imagePath); const url = await imageRef.getDownloadURL(); return { ...item, image: url }; } catch (error) { console.log('Error loading image:', error); return item; } }); setSearchResult(await Promise.all(resultWithImages)); - User
Ошибка загрузки изображения: [TypeError: undefined is not a function]
- Assistant
The error message "TypeError: undefined is not a function" suggests that you might be calling a function on an undefined or null value. From the code you provided, it's difficult to determine what's causing the issue without debugging further.
Here are a few things you can try to investigate the issue:
-
Check if the
imagePathvariable has a valid value. You can add a console.log statement before theconst imageRef = storage.ref().child(imagePath);line to see if the variable is set correctly. -
Make sure that you're calling the
getDownloadURL()method on a valid reference to a Firebase Storage file. You can add a console.log statement after theconst imageRef = storage.ref().child(imagePath);line to see if theimageRefvariable has a valid value. -
Verify that the Firebase Storage SDK is initialized correctly and that you have the necessary permissions to access the files. You can check the Firebase Console to see if there are any errors or permission issues.
-
Try simplifying the code to isolate the issue. For example, you can create a new test function that only retrieves one image and see if that works correctly.
-
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.