[英]Can't get value of Object in Javascript , nodejs, stripe library
Im trying to get a value "status" from "customer" object with this simple script:我试图用这个简单的脚本从“客户”对象中获取一个值“状态”:
console.log(JSON.stringify(customer.subscriptions.data.plan.status));
When i execute this function, the console returns me:当我执行这个函数时,控制台返回给我:
TypeError: Cannot read property 'data' of undefined
"customer" Object: “客户”对象:
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",
...,
Please help me.请帮我。 Thanks.
谢谢。
The error doesn't match up with the data.错误与数据不匹配。 It should be that it can't read
status
of undefined.应该是它无法读取未定义的
status
。 This is because customer
does have subscriptions
, and subscriptions
does have data
, but then you're treating data
as though it had a plan
property, which it doesn't.这是因为
customer
确实有subscriptions
,而subscriptions
确实有data
,但是您将data
视为具有plan
属性,而实际上没有。 data
refers to an array , the first entry of which has a plan
property. data
指的是一个数组,它的第一个条目有一个plan
属性。 Also note that status
is not a property of plan
, it's a property of the same object that plan
is a property of.另请注意,
status
不是plan
的属性,它是plan
是其属性的同一对象的属性。
So accessing the first entry's status
would be:因此,访问第一个条目的
status
将是:
customer.subscriptions.data[0].status
// Note -------------------^^^
If there are subsequent entries in data
, they would be at indexes 1, 2, 3, etc.如果
data
有后续条目,它们将位于索引 1、2、3 等处。
Example:例子:
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.