Tuesday, May 14, 2024
25
rated 0 times [  32] [ 7]  / answers: 1 / hits: 6956  / 2 Years ago, thu, october 13, 2022, 12:00:00

when I console data for the first console.log, it's given me undefined.


somethings likes:


log: undefined

but if I trying to again try to save the file using Ctrl s ,it's working fine and give me values for the API.


enter


here is my code below:


   const fetchPosts = () => {
const fP = devices?.map(async (id) => {
const { data } = await axios.get(`https://jsonplaceholder.typicode.com/users`, myHeaders);
return [data?.data.id];
})
return fP;
};


const { data } = useQuery('post', fetchPosts);
console.log(data);

how can i avoid undefined for the first console.log?


anyone can help me to solve this issues.


Thankyou for your trying in advance!


More From » react-native

 Answers
6

To avoid undefined you need to check for loading and error.
Here's a simple way to fetch list of users.




import { useQuery } from react-query;
import axios from axios;

function Test() {
const fetchUser = async () => {
const response = await axios.get(
https://jsonplaceholder.typicode.com/users
);
return response.data;
};

const { data, isLoading, error } = useQuery(user, fetchUser);

if (isLoading) return Loading...;
if (error) return An error has occurred: + error.message;

return (
<div>
<h1>Users</h1>

{data.map((item) => {
return (
<div key={item.id}>
<h1>{item.name}</h1>
<h1>{item.email}</h1>
</div>
);
})}
</div>
);
}

export default Test;




I notice you have named your function fetchPost. Suppose you want to fetchPost as well. Here's how I would do it in a simple way.




import { useQuery } from react-query;
import axios from axios;

function Test() {
const fetchUser = async () => {
const response = await axios.get(
https://jsonplaceholder.typicode.com/users
);
return response.data;
};

const fetchPosts = async () => {
const response = await axios.get(
https://jsonplaceholder.typicode.com/posts
);
return response.data;
};

const { data, isLoading, error } = useQuery(user, fetchUser);

const {
data: posts,
isLoading: postLoading,
error: postError,
} = useQuery(posts, fetchPosts);

if (isLoading) return Loading...;
if (error) return An error has occurred: + error.message;

if (postLoading) return <h1>Loading...</h1>;
return (
<div>
<h1>*************Users*************</h1>

{data.map((item) => {
return (
<div key={item.id}>
<h1>{item.name}</h1>
<h1>{item.email}</h1>
</div>
);
})}

<h1>*************POST*************</h1>
{posts.map((item) => {
return (
<div key={item.id}>
<h1>{item.title}</h1>
<h1>{item.body}</h1>
</div>
);
})}
</div>
);
}

export default Test;




I hope this answers your question.


[#25] Monday, July 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lonnier

Total Points: 621
Total Questions: 113
Total Answers: 98

Location: Nepal
Member since Mon, Jan 4, 2021
3 Years ago
lonnier questions
Tue, Nov 24, 20, 00:00, 4 Years ago
Sun, Jun 7, 20, 00:00, 4 Years ago
Wed, Jan 8, 20, 00:00, 4 Years ago
;