Apollo useQuery: API is working well but data is undefined

Viewed 1349

I'm using apollo-boost as my client. My useQuery hook works strangely, and I'm asking question here.

const { loading, data, refetch } = useQuery(SEARCH, {
    variables: {
      query: term,
    },
    skip: !shouldFetch,
    fetchPolicy: "network-only",
  });
  console.log(loading, data);

This is my useQuery code. If data is empty, it console logs well. However, if there is some data fetched data becomes undefined.

case 1(if fetched data is empty):

network response:

{data: {readPosts: []}}
data: {readPosts: []}
readPosts: []

console:

false {readPosts: Array(0)}

case 2(if there is fetched data):

network response:

data: {,…}}
data: {,…}
readPosts: [{id: "25", photos: [], title: "예시 제목2", price: 1234, user: {major: "", __typename: "User"},…}]
0: {id: "25", photos: [], title: "예시 제목2", price: 1234, user: {major: "", __typename: "User"},…}

console:

false undefined

It is very weird because it seems there isn't any typo because it works well in case 1. However, how come it works strangely in case 2?

0 Answers
Related