【问题标题】:Cannot read property of "then" of undefined APOLLO GRAPHQL无法读取未定义 APOLLO GRAPHQL 的“then”属性
【发布时间】:2019-05-12 14:18:36
【问题描述】:

我遇到了麻烦,我被卡住了。我曾经在我的其他代码上复制此方法,但此方法不适用于 apollo。这是我在 vue.js 上使用 apollo 的方法。

handleLikePost() {
    const variables = {
        postId: this.postId,
        username: this.user.username
    };
    this.$apollo.mutate({
        mutation: LIKE_POST,
        variables,
        update: (cache, { data: { likePost } }) => {
            const data = cache.readQuery({
                query: GET_POST,
                variables: { postId: this.postId }
            });
            data.getPost.likes += 1;
            cache
                .writeQuery({
                query: GET_POST,
                variables: { postId: this.postId },
                data
            })
                .then(({ data }) => {
                // const updatedUser = {
                    //   ...this.user,
                    //   favorites: data.likePost.favorites
                    // };
                //this.$store.commit("setUser", updatedUser);
                console.log(this.user);
                console.log(data.likePost);
            })
                .catch(err => console.error(err));
        }
    });
}

【问题讨论】:

    标签: vue.js graphql apollo


    【解决方案1】:

    我认为问题是你没有返回一些东西;

    cache.writeQuery()

    这就是为什么.then({data}) 没有从writeQuery() 那里得到任何东西

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-06
      • 2020-02-01
      • 2017-04-21
      • 2020-12-03
      • 2021-02-07
      • 2019-08-19
      • 2016-10-23
      相关资源
      最近更新 更多