i have this err ERROR Error: [TypeError: Cannot read property 'split' of undefined]
this is my home screen
import { ScrollView, Text, View } from 'react-native';
import { gStyle } from '../styles/style';
import Header from '../components/Header';
import Footer from '../components/Footer';
import PastMK from '../components/PastMK';
import FutureMK from '../components/FutureMK';
when i remove FutureMK there is no err anymore but when i add it again here is err. here is my FutureMK
import { Text, View, Image, Pressable } from 'react-native';
import { gStyle } from '../styles/style';
import React, {useState, useEffect} from 'react';
import { useNavigation } from '@react-navigation/native';
import {firebase} from '../Firebase/firebase';
import moment from 'moment';
import 'firebase/compat/storage';
import 'firebase/compat/firestore';
It seems like the error is coming from somewhere else, not from the Home or FutureMK components you've shared. The split method is being called on an undefined property, and from the code you've provided, there's no split method being used.
To identify the issue, you can follow these steps:
Check the console or terminal for the error message and see if you can identify which file or function the error is coming from.
Inspect that file or function, and look for any instances where the split method is being called.
Once you find the issue, check why the property is undefined and handle it accordingly.
For example, if split is being called like this:
const result = someVariable.split();
Make sure that someVariable is defined and has a value before calling the split method on it. You can do this by using conditional (ternary) operators or optional chaining:
const result = someVariable?.split();
User
import { Text, View, Image, Pressable } from 'react-native';
import { gStyle } from '../styles/style';
import React, {useState, useEffect} from 'react';
import { useNavigation } from '@react-navigation/native';
import {firebase} from '../Firebase/firebase';
import moment from 'moment';
import 'firebase/compat/storage';
import 'firebase/compat/firestore';
export default function FutureMK() {
const navigation = useNavigation();
return (
time.toDate
name
Цена: price P.
description
<Pressable style={gStyle.FutureMKmoreDetails}
onPress={()=>{navigation.navigate('SignUpForMK',{mkData});}}
>
Подробнее
</View>
);
}
i need to show users data from firebase collection's name is FutureMK and its fields name, time, image, price, description. Image is string, there is url to firebase storage where the image is
Assistant
User
Assistant
The issue is that you're displaying the FutureMK component twice in your Home screen. To fix this, you just need to remove one of the <FutureMK/> components in the Home screen.
Update your Home screen like this:
import { ScrollView, Text, View } from 'react-native';
import { gStyle } from '../styles/style';
import Header from '../components/Header';
import Footer from '../components/Footer';
import PastMK from '../components/PastMK';
import FutureMK from '../components/FutureMK';
export default function Home() {
return (
<View>
<ScrollView>
<Header />
<Text style={gStyle.header}>Творческие мастер-классы{'\n'}от Белой Розы</Text>
<FutureMK />
<Text style={gStyle.header}>Прошедшие{'\n'}мастер-классы</Text>
<PastMK />
<Footer />
</ScrollView>
</View>
);
}
Now, there will be only one FutureMK component in the Home screen, and your posts should appear twice no more.
User
but if i want to display only the last one post from db?
Assistant
If you want to display only the last post from the database, you can change the fetchData function in your FutureMK component to fetch only the last document from the Firestore collection.
First, you'll need to modify the query to sort the documents based on the timestamp or any other unique attribute that defines the order. Then, you should limit the results to 1, so you only get the last post.
Here's the updated FutureMK component with a modified fetchData function that fetches only the last post:
User
i nedd to show all posts on AllFutureMK screen and only two last on Home screen
Assistant
To achieve this, you can create a new prop for your component called , which will be used to control the number of posts to fetch for each screen. By default, it will fetch all posts, but when used on the screen, you can pass as the prop to fetch only the last two posts.
User
how can i shorten description?
Assistant
You can use JavaScript's substring() method to shorten the description. substring() takes two arguments: the start index (inclusive) and the end index (exclusive). To shorten the description to a desired length, you can pass 0 as the start index and your desired length as the end index.
For example, if you want to limit the description to 100 characters, you can modify the FutureMK component's description rendering as follows:
This will add an ellipsis at the end of the text if the description is longer than 100 characters. You can replace 100 with any other number to limit the description to the desired length.
User
now i have to do that when user click on button it redirects him on another screen where is info and details of particular post
Assistant
To navigate the user to a new screen with full details of a particular post, you can follow these steps:
Create a new screen component that will display the post details, for example, .
OR
const result = someVariable ? someVariable.split() : '';
This should prevent the error from happening. If you still can't identify the issue, please provide more information so we can help you better.
To display the data from the Firebase collection named 'FutureMK', you can follow the approach you had in your previous FutureMK component but with some tweaks.
Here's the updated FutureMK component that fetches data from the 'FutureMK' collection and displays it:
This code fetches data from the 'FutureMK' collection, updates the image URL, sets the fetched data to the futureMKData state, and maps through the data to display it in the app. Make sure your Firebase configurations are set up correctly and the structure of your Firestore data matches the expected fields (name, time, image, price, description).
okay not its working but i had 2 posts and here are 4 of them
import { Text, View, Image, Pressable } from 'react-native';
import { gStyle } from '../styles/style';
import React, {useState, useEffect} from 'react';
import { useNavigation } from '@react-navigation/native';
import {firebase} from '../Firebase/firebase';
import moment from 'moment';
import 'firebase/compat/storage';
import 'firebase/compat/firestore';
here is my home screen
import { ScrollView, Text, View } from 'react-native';
import { gStyle } from '../styles/style';
import Header from '../components/Header';
import Footer from '../components/Footer';
import PastMK from '../components/PastMK';
import FutureMK from '../components/FutureMK';
In this updated version, we added .orderBy('time', 'desc') to sort the documents in descending order based on the 'time' field. We also added .limit(1) to the query to fetch only the last document.
Please note that you should replace 'time' with the correct field name used in your Firestore collection for sorting the documents.
FutureMK
limit
Home
2
limit
Here's the updated FutureMK component with the limit prop:
By doing this, you will fetch all posts on the AllFutureMK screen while fetching only the two most recent posts on the Home screen.
PostDetails.js
In your FutureMK component, use the useNavigation() hook and the onPress prop of the <Pressable> component to navigate to the PostDetails screen and pass the full mkData as a parameter.
In your PostDetails screen, use the route prop to access the passed mkData and display the full details of the post.
Here's an example of how to implement these steps:
Create PostDetails.js:
import React from 'react';
import { View, Text } from 'react-native';
import { gStyle } from '../styles/style';
const PostDetails = ({ route }) => {
const { mkData } = route.params;
return (
<View style={gStyle.main}>
<Text style={gStyle.dateFutureMK}>
{moment(mkData.time.toDate()).format('Do MMM YYYY')}
</Text>
<Image source={{ uri: mkData.image }} style={gStyle.FutureMKbannerImg} />
<Text style={gStyle.FutureMKnameOfMK}>{mkData.name}</Text>
<Text style={gStyle.FutureMKprice}>
Цена: <Text style={gStyle.FutureMKrub}>{mkData.price} P.</Text>
</Text>
<Text
style={gStyle.FutureMKdescription}
>
{mkData.description}
</Text>
{/* Add any other post details that need to be displayed in this screen */}
</View>
);
};
export default PostDetails;
Update the FutureMK component's <Pressable> component:
Add the PostDetails screen to your navigation configuration:
Make sure to import the PostDetails screen and add it to your navigation configuration (for example, in your App.js or StackNavigator file), like this:
import PostDetails from './screens/PostDetails';
// ... other code ...
<Stack.Screen name="PostDetails" component={PostDetails} />
Now, when a user clicks the "Подробнее" button, the app will navigate to the PostDetails screen, displaying the full details of the selected post.