javascript - 动态循环遍历 JSON 时出现未定义错误

标签 javascript json node.js for-loop ejs

我试图在我的 JSON 数据中输出所有客户 ID。我使用 ejs 作为我的模板引擎。到目前为止,我只能通过以下方式成功输出 1 个客户 ID:

<p><%= jsonOrderData.orders[0].customer.id %></p>

当我尝试遍历每个订单时,我得到无法读取未定义的属性“id”

for循环要循环遍历

    <% for (var i = 0; i < jsonOrderData.orders.length; i++) {%>
        <p>Customer: <%= jsonOrderData.orders[i].customer.id %></p>
    <% }; %>

如果我在客户之后删除 .id,错误就会消失。然后输出 Customer:[object Object] json数据长度的50倍

我不明白为什么它在添加 .id 时在循环中不工作,而在没有循环手动设置索引的情况下它工作正常?

JSON 数据(减少 2 个订单)

{
  "orders": [
    {
      "id": 533078016054,
      "email": "email@gmail.com",
      "closed_at": null,
      "created_at": "2018-08-10T05:03:36+01:00",
      "updated_at": "2018-08-10T05:03:37+01:00",
      "number": 52,
      "note": "",
      "token": "f4877048c08eb98180ee5fda34f978bc",
      "gateway": "manual",
      "test": false,
      "total_price": "13.98",
      "subtotal_price": "13.98",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "13.98",
      "cart_token": null,
      "buyer_accepts_marketing": false,
      "name": "#MW1052",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "18.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": null,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-10T05:03:36+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1052,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "email@gmail.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/f4877048c08eb98180ee5fda34f978bc/authenticate?key=redacted",
      "admin_graphql_api_id": "gid://shopify/Order/redacted",
      "line_items": [
        {
          "id": 1350736445494,
          "variant_id": 8725905539126,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss15",
          "variant_title": "S / Bottle Green",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Bottle Green",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736445494",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        },
        {
          "id": 1350736478262,
          "variant_id": 8725905440822,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss12",
          "variant_title": "S / Heather Grey",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Heather Grey",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736478262",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "billing_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "Jogn Doe",
        "country_code": "GB",
        "province_code": null
      },
      "shipping_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "John Doe",
        "country_code": "GB",
        "province_code": null
      },
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 556974014518,
        "email": "email@gmail.com",
        "accepts_marketing": false,
        "created_at": "2018-06-26T00:26:55+01:00",
        "updated_at": "2018-08-10T05:03:36+01:00",
        "first_name": "John",
        "last_name": "Doe",
        "orders_count": 22,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 533078016054,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "",
        "last_order_name": "#MW1052",
        "admin_graphql_api_id": "gid://shopify/Customer/556974014518",
        "default_address": {
          "id": 601657278518,
          "customer_id": 556974014518,
          "first_name": "John",
          "last_name": "Doe",
          "company": null,
          "address1": "17A Oaklands Business Centre",
          "address2": "Elm Grove",
          "city": "Worthing",
          "province": null,
          "country": "United Kingdom",
          "zip": "BN11 5LH",
          "phone": null,
          "name": "John Doe",
          "province_code": null,
          "country_code": "GB",
          "country_name": "United Kingdom",
          "default": true
        }
      }
    },
    {
      "id": 532977778742,
      "email": "james@bungeedesign.com",
      "closed_at": null,
      "created_at": "2018-08-09T22:18:53+01:00",
      "updated_at": "2018-08-09T22:18:53+01:00",
      "number": 51,
      "note": "",
      "token": "a292d75bd7011cf255a1bf236b23d0a5",
      "gateway": "manual",
      "test": false,
      "total_price": "6.99",
      "subtotal_price": "6.99",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "6.99",
      "cart_token": null,
      "buyer_accepts_marketing": true,
      "name": "#MW1051",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "9.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": 1327759385,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-09T22:18:53+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1051,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "james@bungeedesign.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/a292d75bd7011cf255a1bf236b23d0a5/authenticate?key=9322877ce6ce34be2feeb127d73d0f89",
      "admin_graphql_api_id": "gid://shopify/Order/532977778742",
      "line_items": [
        {
          "id": 1350552453174,
          "variant_id": 8725905408054,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss11",
          "variant_title": "S / Black",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Black",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350552453174",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 552537620534,
        "email": "james@bungeedesign.com",
        "accepts_marketing": true,
        "created_at": "2018-06-15T10:44:13+01:00",
        "updated_at": "2018-08-09T22:18:53+01:00",
        "first_name": "James",
        "last_name": "Rogers",
        "orders_count": 18,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 532977778742,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "password page, prospect",
        "last_order_name": "#MW1051",
        "admin_graphql_api_id": "gid://shopify/Customer/552537620534"
      }
    }
  ]
}

最佳答案

您所拥有的运行良好。翻译过程中可能会丢失某些内容,或者 .id 处于另一个级别。我的猜测是您没有按照预期设置变量 jsonOrderData

如果您执行 console.log(jsonOrderData);,您会得到您期望的结果吗?

这很好用:

const json = {
  "orders": [
    {
      "id": 533078016054,
      "email": "email@gmail.com",
      "closed_at": null,
      "created_at": "2018-08-10T05:03:36+01:00",
      "updated_at": "2018-08-10T05:03:37+01:00",
      "number": 52,
      "note": "",
      "token": "f4877048c08eb98180ee5fda34f978bc",
      "gateway": "manual",
      "test": false,
      "total_price": "13.98",
      "subtotal_price": "13.98",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "13.98",
      "cart_token": null,
      "buyer_accepts_marketing": false,
      "name": "#MW1052",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "18.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": null,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-10T05:03:36+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1052,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "email@gmail.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/f4877048c08eb98180ee5fda34f978bc/authenticate?key=redacted",
      "admin_graphql_api_id": "gid://shopify/Order/redacted",
      "line_items": [
        {
          "id": 1350736445494,
          "variant_id": 8725905539126,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss15",
          "variant_title": "S / Bottle Green",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Bottle Green",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736445494",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        },
        {
          "id": 1350736478262,
          "variant_id": 8725905440822,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss12",
          "variant_title": "S / Heather Grey",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Heather Grey",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736478262",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "billing_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "Jogn Doe",
        "country_code": "GB",
        "province_code": null
      },
      "shipping_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "John Doe",
        "country_code": "GB",
        "province_code": null
      },
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 556974014518,
        "email": "email@gmail.com",
        "accepts_marketing": false,
        "created_at": "2018-06-26T00:26:55+01:00",
        "updated_at": "2018-08-10T05:03:36+01:00",
        "first_name": "John",
        "last_name": "Doe",
        "orders_count": 22,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 533078016054,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "",
        "last_order_name": "#MW1052",
        "admin_graphql_api_id": "gid://shopify/Customer/556974014518",
        "default_address": {
          "id": 601657278518,
          "customer_id": 556974014518,
          "first_name": "John",
          "last_name": "Doe",
          "company": null,
          "address1": "17A Oaklands Business Centre",
          "address2": "Elm Grove",
          "city": "Worthing",
          "province": null,
          "country": "United Kingdom",
          "zip": "BN11 5LH",
          "phone": null,
          "name": "John Doe",
          "province_code": null,
          "country_code": "GB",
          "country_name": "United Kingdom",
          "default": true
        }
      }
    },
    {
      "id": 532977778742,
      "email": "james@bungeedesign.com",
      "closed_at": null,
      "created_at": "2018-08-09T22:18:53+01:00",
      "updated_at": "2018-08-09T22:18:53+01:00",
      "number": 51,
      "note": "",
      "token": "a292d75bd7011cf255a1bf236b23d0a5",
      "gateway": "manual",
      "test": false,
      "total_price": "6.99",
      "subtotal_price": "6.99",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "6.99",
      "cart_token": null,
      "buyer_accepts_marketing": true,
      "name": "#MW1051",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "9.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": 1327759385,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-09T22:18:53+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1051,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "james@bungeedesign.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/a292d75bd7011cf255a1bf236b23d0a5/authenticate?key=9322877ce6ce34be2feeb127d73d0f89",
      "admin_graphql_api_id": "gid://shopify/Order/532977778742",
      "line_items": [
        {
          "id": 1350552453174,
          "variant_id": 8725905408054,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss11",
          "variant_title": "S / Black",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Black",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350552453174",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 552537620534,
        "email": "james@bungeedesign.com",
        "accepts_marketing": true,
        "created_at": "2018-06-15T10:44:13+01:00",
        "updated_at": "2018-08-09T22:18:53+01:00",
        "first_name": "James",
        "last_name": "Rogers",
        "orders_count": 18,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 532977778742,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "password page, prospect",
        "last_order_name": "#MW1051",
        "admin_graphql_api_id": "gid://shopify/Customer/552537620534"
      }
    }
  ]
};

for (let i = 0; i < json.orders.length; i++) {
    const order = json.orders[i];
    console.log(order.customer.id);
}

关于javascript - 动态循环遍历 JSON 时出现未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51869661/

相关文章:

node.js - 运行 "npm"返回 "Error: Cannot find module ' 继承'”

node.js - Mongoose 查询不返回值

javascript - 如何在 nodejs 中组织与 DB 的交互?

javascript - plotly : I want to include some pattern in the bars of a bar chart

javascript - 将时间值从 jquery 发送到 python 服务器

javascript - Internet Explorer 8 的 CSS 故障排除

javascript - 将列搜索应用于当前 jQuery DataTable

php - PHP 会自动将数字转换为字符串吗?

php - 如何在php中解析mysql中的空间数据

json - jq 以科学记数法重新格式化小数——这可以避免吗?