【问题标题】:Node api requests returns array, but error when trying to use indexNode api请求返回数组,但尝试使用索引时出错
【发布时间】:2016-11-01 16:54:28
【问题描述】:

在 node.js 中发出 get 请求时,我在尝试从数组返回索引值时遇到问题。这是使用 shopify 模块,但我相信这将适用于所有获取请求。 我的功能是:

ShopifyObj.Shopify.get('/admin/products.json',  function(err, data, headers){
  console.log(data.products[0]);      
});

以上返回:

"console.log(data.products[0]);
TypeError:无法读取未定义的属性“0”

然而;当我尝试在没有索引的情况下使用 console.log 时,我得到一个数组:

ShopifyObj.Shopify.get('/admin/products.json',  function(err, data, headers){
  console.log(data.products);      
});

//returns
 [ { id: 6560603013,
title: 'Dog',
body_html: 'A cute dog',
vendor: 'Test store 1994 1994',
product_type: '',
created_at: '2016-10-27T11:11:32-04:00',
handle: 'dog',
updated_at: '2016-10-27T11:11:33-04:00',
published_at: '2016-10-27T11:11:00-04:00',
template_suffix: null,
published_scope: 'global',
tags: '',
variants: [ [Object] ],
options: [ [Object] ],
images: [ [Object] ],
image:
 { id: 16395126725,
   product_id: 6560603013,
   position: 1,
   created_at: '2016-10-27T11:11:33-04:00',
   updated_at: '2016-10-27T11:11:33-04:00',
   src: 'https://cdn.shopify.com/s/files/1/1569/4167/products/imagejpeg_0_5.jpg?v=1477581093',
   variant_ids: [] } },
  { id: 6560596805,
title: 'Long sleeve tshirt',
body_html: 'Super long sleeves',
vendor: 'Test store 1994 1994',
product_type: '',
created_at: '2016-10-27T11:10:45-04:00',
handle: 'long-sleeve-tshirt',
updated_at: '2016-10-27T11:13:56-04:00',
published_at: '2016-10-27T11:10:00-04:00',
template_suffix: null,
published_scope: 'global',
tags: 'dog, short-sleeve-t-shirt',
variants: [ [Object] ],
options: [ [Object] ],
images: [ [Object] ],
image:
 { id: 16395113157,
   product_id: 6560596805,
   position: 1,
   created_at: '2016-10-27T11:10:47-04:00',
   updated_at: '2016-10-27T11:10:47-04:00',
   src: 'https://cdn.shopify.com/s/files/1/1569/4167/products/profilepic.jpg?v=1477581047',
   variant_ids: [] } },
  { id: 6560275461,
title: 'Short Sleeve T-Shirt',
body_html: 'A nice t-shirt',
vendor: 'Test store 1994 1994',
product_type: '',
created_at: '2016-10-27T10:38:58-04:00',
handle: 'short-sleeve-t-shirt',
updated_at: '2016-10-27T10:38:59-04:00',
published_at: '2016-10-27T10:38:00-04:00',
template_suffix: null,
published_scope: 'global',
tags: '',
variants: [ [Object] ],
options: [ [Object] ],
images: [ [Object] ],
image:
 { id: 16394553413,
   product_id: 6560275461,
   position: 1,
   created_at: '2016-10-27T10:38:59-04:00',
   updated_at: '2016-10-27T10:38:59-04:00',
   src: 'https://cdn.shopify.com/s/files/1/1569/4167/products/World-Wide-Web.jpg?v=1477579139',
   variant_ids: [] } } ]

我觉得我在异步方面做错了什么,但我无法弄清楚。当 console.log 的 settimeout() 几秒钟后,它将正确显示。有没有什么方法可以得到我想要的结果,而不必在任意时间内使用 settimeout?

【问题讨论】:

    标签: javascript node.js shopify


    【解决方案1】:

    您收到的无疑是 JSON 字符串。 为了将数据用作数组对象,您需要先对其进行解析。

    var data = JSON.parse(data.products);
    console.log(data[0]);
    

    【讨论】:

    • 感谢您的反馈,但是当我这样做时,我得到 [object Object] ^ SyntaxError: Unexpected token o in JSON at position 1 at Object.parse (native)
    • 我正要添加这个,但我没有:如果失败 - 尽管它不应该 - 你可以尝试 ` var data = Function( "return"+data.products)(); console.log(data[0]);` 但这仅用于调试目的!直到您解决返回的字符串格式的问题。
    • 抱歉,当我尝试执行 var data = JSON.parse(data) 时出现 [object Object] 错误。当我尝试做 JSON.parse(data.products) 我得到 undefined ^ SyntaxError: Unexpected token u in JSON at position 0 at Object.parse (native) 和你的函数我得到 returnundefined ^ ReferenceError: returnundefined is not defined跨度>
    • 这是我无法理解的,console.log(data.products instanceof Object) 返回 true,但是 console.log(data.products instaceof Array) 返回 false。
    • 您需要将您的data.products 的确切返回发送给我们,以查看该字符串是否为有效的 JSON 数组。而且您很可能需要使用您正在使用的 API 解析器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多