-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyelpAPItest.js
85 lines (71 loc) · 2.75 KB
/
yelpAPItest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const yelp = require('yelp-fusion');
const mySecretInfo = require('./env.js');
const mySearchBusinessName = 'starbucks';
const mySearchBusinessLocation = 'aurora, co';
const searchRequest = {
term: mySearchBusinessName,
location: mySearchBusinessLocation
};
function useYelpApi() {
console.log("running yelp api function");
yelp.accessToken(mySecretInfo.clientId, mySecretInfo.clientSecret).then(response => {
const client = yelp.client(response.jsonBody.access_token);
client.search(searchRequest).then(response => {
const firstResult = response.jsonBody.businesses[0];
const firstResultJson = JSON.stringify(firstResult, null, 4);
console.log(firstResultJson);
const secondResult = response.jsonBody.businesses[1];
const secondResultJson = JSON.stringify(secondResult, null, 4);
console.log(secondResultJson);
const thirdResult = response.jsonBody.businesses[2];
const thirdResultJson = JSON.stringify(thirdResult, null, 4);
console.log(thirdResultJson);
const fourthResult = response.jsonBody.businesses[3];
const fourthResultJson = JSON.stringify(fourthResult, null, 4);
console.log(fourthResultJson);
const fifthResult = response.jsonBody.businesses[4];
const fifthResultJson = JSON.stringify(fifthResult, null, 4);
console.log(fifthResultJson);
});
}).catch(e => {
console.log(e);
});
}
useYelpApi();
app.post('/searchResults', function(req,res) {
var businessResultsList = [];
var mySearchBusinessName = req.body.businessName;
var mySearchBusinessLocation = req.body.businessCity;
console.log(mySearchBusinessName);
console.log(mySearchBusinessLocation);
var searchRequest = {
term: mySearchBusinessName,
location: mySearchBusinessLocation
};
console.log(searchRequest);
console.log('about to run the yelpAPIcall function');
function runYelpAPICall() {
console.log("running yelp api function");
yelp.accessToken(mySecretInfo.clientId, mySecretInfo.clientSecret).then(response => {
const client = yelp.client(response.jsonBody.access_token);
client.search(searchRequest).then(response => {
var jsonifiedBody = JSON.parse(response.body);
var businesses = jsonifiedBody.businesses;
for(i = 0; i < businesses.length; i++) {
var business = {
name: businesses[i].name,
image: businesses[i].image_url,
yelp: businesses[i].url,
address: businesses[i].location.display_address
};
businessResultsList.push(business);
}
console.log(businessResultsList);
res.json(businessResultsList);
//res.render('searchResults', {businessResultsList: businessResultsList});
});
});
};
runYelpAPICall();
});