访问从 Meteor 中的 Stripe API 返回的 JSON 字典中的值
Access value in JSON dictionary returned from Stripe API in Meteor
当 Stripe oAuth 进程失败时,Stripe API 返回给我一个 JSON 字典(不管是什么)。我已将其存储为变量,但无法访问其中的值 "error_description"。 Here is a link to the relevant Stripe docs
不确定这是否有所不同,但这是在服务器端方法中的 Meteor 应用程序中。
我将以下内容存储为一个名为 "error" 的变量。这是我 console.log(error) 时得到的;
{ [Error: failed [400] { "error": "invalid_grant", "error_description": "Authorization code does not exist: ac_7z4pnJ8ee71yJNxguV6wiINIf10fgl6j" }] stack: [Getter] }
我的问题是:如何访问 "error_description"?
我写的时候好像不行
var newError = error.error_description
如果有人能帮我弄到 error_description 那就太好了,谢谢!
更新
添加 .message 删除了部分响应,但我仍然无法 JSON.parse 它或访问 error_description
var error = error.message 结果为
failed [400] { "error": "invalid_grant", "error_description": "Authorization code does not exist: ac_7z4pnJ8ee71yJNxguV6wiINIf10fgl6j" }
您似乎收到了 error object。
您应该可以使用:
error.message
那部分似乎是一个 JSON 字符串,但它以 "failed [400] " 之类的文本为前缀,这不是后面 JSON 的一部分。所以你应该剥离第一部分以获得 JSON 字符串,如下所示:
var json = /(\{.+)/.exec(error.message)[1]; // strip prefix
var errorObj = JSON.parse(json);
console.log(errorObj.error_description);
Stripe API 返回给我一个 JSON 字典(不管是什么)。我已将其存储为变量,但无法访问其中的值 "error_description"。 Here is a link to the relevant Stripe docs
不确定这是否有所不同,但这是在服务器端方法中的 Meteor 应用程序中。
我将以下内容存储为一个名为 "error" 的变量。这是我 console.log(error) 时得到的;
{ [Error: failed [400] { "error": "invalid_grant", "error_description": "Authorization code does not exist: ac_7z4pnJ8ee71yJNxguV6wiINIf10fgl6j" }] stack: [Getter] }
我的问题是:如何访问 "error_description"?
我写的时候好像不行
var newError = error.error_description
如果有人能帮我弄到 error_description 那就太好了,谢谢!
更新 添加 .message 删除了部分响应,但我仍然无法 JSON.parse 它或访问 error_description
var error = error.message 结果为
failed [400] { "error": "invalid_grant", "error_description": "Authorization code does not exist: ac_7z4pnJ8ee71yJNxguV6wiINIf10fgl6j" }
您似乎收到了 error object。
您应该可以使用:
error.message
那部分似乎是一个 JSON 字符串,但它以 "failed [400] " 之类的文本为前缀,这不是后面 JSON 的一部分。所以你应该剥离第一部分以获得 JSON 字符串,如下所示:
var json = /(\{.+)/.exec(error.message)[1]; // strip prefix
var errorObj = JSON.parse(json);
console.log(errorObj.error_description);