1
ノードでget要求を行うときに配列からインデックス値を返そうとして問題が発生します。これはshopifyモジュールを使用していますが、これはすべてのget要求に適用されると思います。 My機能は次のとおりです。ノードapi要求は配列を返しますが、インデックスを使用しようとするとエラーが発生します
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を使わなくても、私の望む結果を得る方法はありますか?
私は、[オブジェクトのオブジェクト]を得ることを行うとき、私はしかし、あなたのフィードバックを感謝し ^ にSyntaxError:(ネイティブ)Object.parseの位置1 でJSONでO予期しないトークンが –
私は追加するgooingましたこれは、私はしませんでした: これが失敗した場合 - 試してみることはできません。 'var data = Function(" return "+ data.products)(); console.log(data [0]); ' これはデバッグのためのものです。返される文字列形式の問題を解決するまで。 –
申し訳ありませんが、var data = JSON.parse(data)を実行しようとすると[object object]エラーが発生します。 Object.parse(ネイティブ)の位置0 でJSONで予期しないトークンuとし、あなたの機能のために、私は ^ にReferenceErrorをreturnundefinedます:私は、私は未定義 ^ にSyntaxErrorを取得JSON.parse(data.products)を実行しようとすると:returnundefinedが定義されていません –