我试图获得一个价值"状态"来自"客户"使用这个简单的脚本对象:
console.log(JSON.stringify(customer.subscriptions.data.plan.status));
当我执行此功能时,控制台会返回我:
TypeError: Cannot read property 'data' of undefined
"顾客"对象:
customer: {
"object":"customer",
"created":xxxxxx,
"id":"xxxxxxx",
"livemode":false,
"description":null,
"email":"xxxx@xxxx.com",
"shipping":null,
"delinquent":false,
"metadata":{},
"subscriptions":{
"object":"list",
"total_count":1,
"has_more":false,
"url":"/v1/customers/xxxxxxxxx/subscriptions",
"data":[{
"id":"xxxxxxxxx",
"plan":{
"interval":"month",
"name":"xxxxxx",
"created":xxxxx,
"amount":xxxxx,
"currency":"eur",
"id":"6month",
"object":"plan",
"livemode":false,
"interval_count":6,
"trial_period_days":null,
"metadata":{},
"statement_descriptor":null,
"statement_description":null},
"object":"subscription",
"start":xxxxx,
"status":"active",
...,
请帮帮我。 感谢。
答案 0 :(得分:4)
错误与数据不匹配。应该是它无法读取未定义的status
。这是因为customer
确实有subscriptions
,而subscriptions
确实有data
,但是您正在处理data
好像它有一个plan
属性,但它并没有。 data
指的是数组,其第一个条目具有plan
属性。另请注意,status
不是plan
的属性,它是plan
属性的同一对象的属性。
因此,访问第一个条目的status
将是:
customer.subscriptions.data[0].status
// Note -------------------^^^
如果data
中有后续条目,则它们将位于索引1,2,3等处。
示例:
var customer = {
"object": "customer",
"created": "xxxxxx",
"id": "xxxxxxx",
"livemode": false,
"description": null,
"email": "xxxx@xxxx.com",
"shipping": null,
"delinquent": false,
"metadata": {},
"subscriptions": {
"object": "list",
"total_count": 1,
"has_more": false,
"url": "/v1/customers/xxxxxxxxx/subscriptions",
"data": [
{
"id": "xxxxxxxxx",
"plan": {
"interval": "month",
"name": "xxxxxx",
"created": "xxxxx",
"amount": "xxxxx",
"currency": "eur",
"id": "6month",
"object": "plan",
"livemode": false,
"interval_count": 6,
"trial_period_days": null,
"metadata": {},
"statement_descriptor": null,
"statement_description": null
},
"object": "subscription",
"start": "xxxxx",
"status": "active"
}
]
}
};
document.body.innerHTML = customer.subscriptions.data[0].status;