0

I'm trying to use the result I'm getting from my apollo client. but it keeps giving me error of undefined although the request send was success in network. It means I cannot get access to the result like normal! the code in my store is:

async questionSearch({ commit }, data) {
const ALL_QUESTION_SEARCH = gql`
  query searchQuestion(
    $token: String
    $bookId: String!
    $page: Int
    $keyword: String
    $field: String
    $source: String
    $fromYear: Int
    $toYear: Int
    $topic: String
    $minDifficultyLevelId: Int
    $maxDifficultyLevelId: Int
    $starred: Boolean
  ) {
    searchQuestion(
      args: {
        token: $token
        bookId: $bookId
        page: $page
        keyword: $keyword
        field: $field
        source: $source
        fromYear: $fromYear
        toYear: $toYear
        topic: $topic
        minDifficultyLevelId: $minDifficultyLevelId
        maxDifficultyLevelId: $maxDifficultyLevelId
        starred: $starred
      }
    ) {
      question {
        information {
          bookId
          topic
          isRoot
          childCount
          parentId
        }
        stats {
          like
          difficultyLevelId
        }
        source {
          name
          publishYear
          field
        }
        content {
          question
          type
          answers {
            id
            text
            isTrue
          }
        }
        key {
          description
        }
        rowNumber
        sortingInformation {
          season
          section
          subSection
          index
        }
        starred
        id
        isAnswered
        answers {
          bookId
          questionId
          answerId
          isTrue
          timestamp
          userId
        }
      }
    }
  }
`
const { result, error } = useQuery(ALL_QUESTION_SEARCH, {
  bookId: data.bookId,
  page: data.page,
  keyword: data.keyword,
  field: data.field,
  source: data.source,
  fromYear: data.fromYear,
  toYear: data.toYear,
  topic: data.topic,
  minDifficultyLevelId: data.minDifficultyLevelId,
  maxDifficultyLevelId: data.maxDifficultyLevelId,
  starred: data.starred,
  token: localStorage.getItem('token'),
})
// const response = useResult(result)
commit('questions', result)
commit('setSuccess', error)

},

and I'm calling my function like this:

async searchQuestions() {
  let form = {
    bookId: this.id,
    page: this.page || '',
    keyword: this.keyword || '',
    field: this.field || '',
    source: this.source || '',
    fromYear: this.fromYear || 1364,
    toYear: this.toYear || 1402,
    topic: this.book_topic || '',
    minDifficultyLevelId: this.minDifficultyLevelId || 0,
    maxDifficultyLevelId: this.maxDifficultyLevelId || 40,
    difficultyLevel: this.difficultyLevel || 40,
    starred: this.starred || false,
  }
  await this.questionSearch(form).then(() => {
    console.log(this.getQuestions)
    this.questionList = this.getQuestions.question
    console.log(this.questionList)
  })
},

But even in console.log it returns undefined.

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'question')

How should I be able to access my data and display it in my template then? the actual console of my result in store is like: enter image description here

2
  • You are not actually awaiting any result from this.questionSearch and your then() code is running immediately. Your async function should return a Promise that resolves based on your query result. Commented Nov 10, 2023 at 17:37
  • You may want to use useLazyQuery instead since it returns a Promise Commented Nov 10, 2023 at 17:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.