[英]Can't get value of Object in Javascript , nodejs, stripe library
我试图用这个简单的脚本从“客户”对象中获取一个值“状态”:
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",
...,
请帮我。 谢谢。
错误与数据不匹配。 应该是它无法读取未定义的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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.