question

mediawranglers avatar image
0 Likes"
mediawranglers asked ·

Adding new variable to Javascript / JSON "Getting Started Tutorial"

Hi there! In learning about the eBay API I came across this tutorial on findItemsByKeywords: [ http://developer.ebay.com/Devzone/finding/HowTo/GettingStarted_JS_NV_JSON/GettingStarted_JS_NV_JSON.html][1] It works fine (obviously) but when I try to add a new variable (price) the call returns "undefined". The section of code I am modifying is (note variable "price" on lines 12 and 14): // Parse the response and build an HTML table to display search results function _cb_findItemsByKeywords(root) { var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || []; var html = []; html.push(' '); for (var i = 0; i < items.length; ++i) { var item = items[i]; var title = item.title; var pic = item.galleryURL; var viewitem = item.viewItemURL; **var price = item.buyItNowPrice;** if (null != title && null != viewitem) { html.push(' ' + '' + '' + ' ' + **price** + '' + ' ' + title + ''); } } html.push(' '); document.getElementById("results").innerHTML = html.join(""); } // End _cb_findItemsByKeywords() function I have tried a bunch of variations of item.currentPrice, item.convertedCurrentPrice, item.item.convertedBuyItNowPrice, etc. All of them return "undefined" in the html table output. In fact, I cannot seem to make any of the other fields the call returns (like conditionID, categoryID, etc) pass to a variable - they all display as "undefined". I'm sure I'm missing something basic here but I can't think what - I would appreciate any pointers or hints you can share with me. Below is the full code if you want to see the result with the "undefined" variable - note that I have replaced my Key with "APPID" because I am not sure if that should be shared publicly. Thanks for any and all help!

eBay Search Results

finding apijsoncurrentprice
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
yazhou_developersupport avatar image
0 Likes"
yazhou_developersupport answered ·
Hello mediawranglers , Please try below JS: function _cb_findItemsByKeywords(root) { var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || []; var html = []; html.push(' '); for (var i = 0; i < items.length; ++i) { var item = items[i]; var title = item.title; var pic = item.galleryURL; var viewitem = item.viewItemURL; var currency = item.sellingStatus[0].currentPrice[0]["@currencyId"]; var price = item.listingInfo[0].buyItNowAvailable[0] == "true" ? currency + item.listingInfo.buyItNowPrice : (currency + item.sellingStatus[0].currentPrice[0].__value__); if (null != title && null != viewitem) { html.push(' ' + '' + '' + ' ' + title + '' + price + ''); } } html.push(' '); document.getElementById("results").innerHTML = html.join(""); } // End _cb_findItemsByKeywords() function
· Share
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.