从 apollo 客户端错误获取结果 undefined

Get Result from apollo client error undefined

提问人:sanaz baghban 提问时间:11/10/2023 更新时间:11/10/2023 访问量:28

问:

我正在尝试使用我从阿波罗客户端获得的结果。但是它一直给我未定义的错误,尽管请求在网络中成功发送。这意味着我无法像往常一样获得结果! 我商店中的代码是:

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)

},

我像这样调用我的函数:

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)
  })
},

但即使在 console.log 中,它也会返回 undefined。

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

那么,我应该如何访问我的数据并将其显示在我的模板中?我在商店中的结果的实际控制台是这样的: 在此处输入图像描述

vue.js graphql vuex apollo-client vue-apollo

评论

0赞 yoduh 11/11/2023
您实际上并没有等待任何结果,您的代码正在立即运行。异步函数应返回一个 Promise,该 Promise 会根据查询结果进行解析。this.questionSearchthen()
0赞 yoduh 11/11/2023
您可能希望改用 useLazyQuery,因为它返回一个 Promise

答: 暂无答案