t3chnoboy / amazon-product-api

:credit_card: Amazon Product Advertising API client
366 stars 104 forks source link

BrowseNodeLookup is not working as expected #22

Closed Codepoet77 closed 9 years ago

Codepoet77 commented 9 years ago

BrowseNodeLookup is not working as expected. This my code below:

var credentials = {
    awsId: 'myId',
    awsSecret: 'mysecret',
    awsTag: 'mytag'
  };

amazon.createClient(credentials).browseNodeLookup({}, function (err, data) {

    console.log(arguments);

    if (err) {
      res.send(err);
    }

    res.send(data);
}

It appears as though I get data back but I see this error in my console when I run my express NodeJS app on line 28:54 in index.js

Ill follow up with more info later if required.

masterT commented 9 years ago

@Codepoet77 what does the error say?

kaue commented 9 years ago

@Codepoet77 The field BrowseNodeId is required to use this method.

Codepoet77 commented 9 years ago

I've updated this issue with the error message I see now.

http://docs.aws.amazon.com/AWSECommerceService/latest/DG/BrowseNodeLookup.html

var express = require('express'); var router = express.Router(); var amazon = require('amazon-product-api');

router.get('/', function (req, res) {

var credentials = { awsId: 'myId', awsSecret: 'mySecret', awsTag: 'myTag' };

amazon.createClient(credentials).browseNodeLookup({}, function (err, data) {

console.log(arguments);

if (err) {
  res.send(err);
}

res.send(data);

});
});

module.exports = router;

This is the error I see: TypeError: Cannot read property '0' of undefined at /home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/lib/index.js:28:54 at Parser. (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/lib/xml2js.js:460:18) at Parser.emit (events.js:95:17) at Object.onclosetag (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/lib/xml2js.js:421:26) at emit (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/node_modules/sax/lib/sax.js:625:33) at emitNode (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/node_modules/sax/lib/sax.js:630:3) at closeTag (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/node_modules/sax/lib/sax.js:874:5) at Object.write (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/node_modules/sax/lib/sax.js:1314:29) at Parser.exports.Parser.Parser.parseString (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/lib/xml2js.js:479:31) at Parser.parseString (/home/andrew/Documents/ppi2/source/server/node_modules/amazon-product-api/node_modules/xml2js/lib/xml2js.js:7:59)

Thanks in advance.

Codepoet77 commented 9 years ago

Quick update. This appears to be a legit issue.

@kauegimenes thanks for the tip. You are right I thought there was a concept of root node but it appears as though that doesnt exist and each major category is a root node. Let me know if my understanding is incorrect.

All that being said below is response that I logged in index.js. I believe the issue we are treating the a browseNodeLookup response like a Item response. The items collections does not exist it in a browse node response and is why I am still getting the exception I listed above.

index.js line 28: results = resp[method + 'Response'].Items[0].Item;

browseNodeResponse: { BrowseNodeLookupResponse: { '$': { xmlns: 'http://webservices.amazon.com/AWSECommerceService/2011-08-01' }, OperationRequest: [ { RequestId: [ '96085264-4de7-11e5-96d5-e745bd0a6445' ], Arguments: [ { Argument: [ { '$': { Name: 'AWSAccessKeyId', Value: 'AKIAIWM6SAEG6BZXFT7Q' } }, { '$': { Name: 'AssociateTag', Value: 'fr056-20' } }, { '$': { Name: 'BrowseNodeId', Value: '9479199011' } }, { '$': { Name: 'Operation', Value: 'BrowseNodeLookup' } }, { '$': { Name: 'ResponseGroup', Value: 'BrowseNodeInfo' } }, { '$': { Name: 'Service', Value: 'AWSECommerceService' } }, { '$': { Name: 'Signature', Value: 'pFHRNSBafAhFj8CoQETsg27MnPTFkPW/YdlNsSzx9B8=' } }, { '$': { Name: 'Timestamp', Value: '2015-08-29T00:47:55.230Z' } } ] } ], RequestProcessingTime: [ '0.007022995' ] } ], BrowseNodes: [ { Request: [ { IsValid: [ 'True' ], BrowseNodeLookupRequest: [ { BrowseNodeId: [ '9479199011' ], ResponseGroup: [ 'BrowseNodeInfo' ] } ] } ], BrowseNode: [ { BrowseNodeId: [ '9479199011' ], Name: [ 'Luggage & Travel Gear' ], Children: [ { BrowseNode: [ { BrowseNodeId: [ '360832011' ], Name: [ 'Backpacks' ] }, { BrowseNodeId: [ '2204831011' ], Name: [ 'Briefcases' ] }, { BrowseNodeId: [ '166767011' ], Name: [ 'Diaper Bags' ] }, { BrowseNodeId: [ '2477385011' ], Name: [ 'Fashion Waist Packs' ] }, { BrowseNodeId: [ '15743181' ], Name: [ 'Gym Bags' ] }, { BrowseNodeId: [ '9971584011' ], Name: [ 'Laptop Bags' ] }, { BrowseNodeId: [ '15743251' ], Name: [ 'Luggage' ] }, { BrowseNodeId: [ '15743231' ], Name: [ 'Messenger Bags' ] }, { BrowseNodeId: [ '15743971' ], Name: [ 'Travel Accessories' ] }, { BrowseNodeId: [ '15744111' ], Name: [ 'Umbrellas' ] } ] } ], Ancestors: [ { BrowseNode: [ { BrowseNodeId: [ '7141124011' ], Name: [ 'Departments' ], IsCategoryRoot: [ '1' ], Ancestors: [ { BrowseNode: [ { BrowseNodeId: [ '7141123011' ], Name: [ 'Clothing, Shoes & Jewelry' ] } ] } ] } ] } ] } ] } ] } }

Codepoet77 commented 9 years ago

Was looking into this a little further and the promise works but the call back does not support browsenodelookup.

masterT commented 9 years ago

I'll dive into it today!

Codepoet77 commented 9 years ago

As mentioned before I am new Git and GitHub and was trying to learn how to fork and pull request but unfortunately need to continue to make progress on my project. This is change I made to make it work in index.js

In the call back only.

parseXML(body, function (err, resp) { if (method === 'BrowseNodeLookup' && resp[method + 'Response'].BrowseNodes && resp[method + 'Response'].BrowseNodes.length > 0) { results = resp[method + 'Response'].BrowseNodes[0].BrowseNode; } else { results = resp[method + 'Response'].Items[0].Item; } cb(null, results); });

The change is I added the if block.

masterT commented 9 years ago

Make a pull request then! It's bloody easy!

  1. Fork the repo
  2. Change de code (make sure the tests pass with npm test)
  3. Commit your changes and push it
  4. Go on this repo in pull request tab and create a new PR. 😊
masterT commented 9 years ago

This may help! https://guides.github.com/activities/forking/

masterT commented 9 years ago

I suggest you to get familiar with the markdown syntaxe. It is very useful since you can use it to write issue and comment.

Codepoet77 commented 9 years ago

OK thanks for all the info! I think I've managed to submit a PR.

Codepoet77 commented 9 years ago

Travis build failed with the same errors that others have seen in the past. Do you have any insight into what got it working again? I don't believe my change caused this (as far as I know).

masterT commented 9 years ago

Fix in PR #23