【发布时间】:2017-12-25 05:48:14
【问题描述】:
我正在使用 Vue.js 和 Vue-Apollo 并启动用户突变来登录用户。我正在使用 graph.cool 服务。
我设置了一个请求管道函数来捕获一些错误,例如无效的电子邮件。
当使用错误/无效输入发出请求时,我的错误catch() 会触发(如预期的那样),并且在网络选项卡中我可以看到自定义错误消息的 JSON。但是,如果从 graph.cool 触发错误,我如何从 catch 中访问这些错误/响应?
示例:
signin () {
const email = this.email
const password = this.password
this.$apollo.mutate({
mutation: signinMutation,
variables: {
email,
password
}
})
.then((data) => {
// This never fires on an error, so I can't
// show the user the errors in the network repsonse.
console.log(data)
})
.catch((error) => {
// Error in this part fires in the console
// but I'm unable to show the JSON response
// errors because the 'then()' above doesn't execute.
console.error(error)
})
}
对于无法识别的用户,我收到以下错误:
错误:GraphQL 错误:找不到具有该信息的用户 在新的 ApolloError (eval at (app.js:956), :34:28) 在评估(评估在(app.js:1353),:139:33) 在
知道如何在catch() 中显示响应中的错误吗?
我可以从字面上看到我想在网络选项卡上的响应中向用户显示的错误:
...但我不知道该怎么做。
非常感谢任何帮助!谢谢。
【问题讨论】:
-
我想你可以在
.catch之后再添加一个.then:stackoverflow.com/questions/35999072/… -
虽然这是真的,但我无法在实际响应本身中获取“数据”。 :(
标签: javascript vue.js graphql apollo vue-apollo