I’m building a simple React Native mobile app for online orders. I store all product-relevant information in Firestore documents as fields (name, price, image URL, etc.). I store the images in Firebase Storage. When I want to create an image reference, I use
const imageRef = storage().refFromURL('gs://testapp-1x2y3z.firebase.com/pics/TestImage.jpg');
The program also works correctly with a static string value. However, when I want to read ImageURL from Firestore document fields e.g.
const assortmentRef = firestore().collection('Test_Store').doc('Assortment');,
imageRef = storage().refFromURL(assortmentRef.ImageURL);
an error is triggered even though the string value of the
assortmentRef.ImageURL
has exactly the value of
gs://testapp-1x2y3z.firebase.com/pics/TestImage.jpg I
assume the problem is because reactive native has asynchronous operations. Does anyone have a solution for this? My goal is to enter dynamic values that I can derive from document fields instead of static URLs. Error message:
Possible Unhandled Promise Rejection (id: 45):
Error: firebase.storage().refFromURL(*) 'url' must be a string value and begin with 'gs://' or 'https://'.
Whole code:
const ReadAssortment = ({ navigation }) => {
const [shoeData, setShoeData] = useState([]);
const fetchData = async () => {
const assortmentRef = firestore().collection('Test_Store').doc('Assortment');
const shoesCollection = assortmentRef.collection('Shoes');
try {
const querySnapshot = await shoesCollection.get();
const dataPromises = [];
querySnapshot.forEach((doc) => {
const imgURL = doc.data().pictureURL;
console.log(imgURL);
const imageRef = storage().refFromURL(imgURL);
dataPromises.push(
imageRef.getDownloadURL().then((url) => ({
...shoeData,
imageUri: url,
}))
);
});
const shoeDataWithImages = await Promise.all(dataPromises);
setShoeData(shoeDataWithImages);
} catch (error) {
console.error('Error fetching:', error);
}
};
useEffect(() => {
fetchData();
}, []);
return (
<View style={styles.container}>
{shoeData.map((shoe, index) => (
<View key={index}>
<Text>Name: {shoe.Name}</Text>
<Text>Price: {shoe.Preis}</Text>
<Text>Info: {shoe.InfoText}</Text>
{shoe.imageUri && <Image source={{ uri: shoe.imageUri }} style={styles.image} />}
</View>
))}
<Button title="Back" onPress={() => navigation.goBack()} />
</View>
);
};
Regards,
Bob
And yes, I tried to parse the value of ImageURL within single and double quotation marks, but it simply doesn’t work. I would be very grateful for any suggestion for a solution.
2
Answers
I solved it that way:
If I correctly understand your question you need to fetch the Firestore Document.
With const
assortmentRef = firestore().collection('Test_Store').doc('Assortment');
you only define aDocumentReference
You need to call the
get()
method of thisDocumentReference
, as explained in the documentation: Something along the folllowing lines (withasync
/await
syntax, while the documentation shows thethen()
syntax)