I am working on a node.js server and want to call the Google Shopping Service and retrieve results for a search query. I think I am applying the search query string correctly, but I am getting a 500 Reference Error: Request is not defined. My code is below:
googleShoppingService.js
//Shopping Search Factory
module.exports = function(productSearch){
return new productKey('---API-KEY-GOES-HERE---');
};
//constructor
function productKey(key) {
this.key=key;
}
// Search API
function toJSON(str){
var result;
try {
result=JSON.parse(str);
} catch(err){}
return result;
}
productKey.prototype.search = function (req, done) {
request.get('https://www.googleapis.com/shopping/search/v1/public/products')
.send({key:this.key})
.send({country:'US'})
.send({q:req.body.searchQuery.value})
.end(function (err, res) {
var body = toJSON(res.text);
var result = body && body.product || [];
return done(err, result);
});
};
app.js
var productSearch = require('./apis/googleShoppingSearch.js');
var products = productSearch('---API-KEY-GOES-HERE---')
app.get('/productResults', function(req, res){
products.search(process.argv[2], function (err, results) {
var returnedData = ""
for (var i=0;i < results.length; i++) {
returnedData += (results[i].title + '<br />')
}
res.send (returnedData)
});
});
Any suggestions to solve my problem would be greatly appreciated. Thanks for the help in advance.