Manipulate data of yelp fusion Business Search with Handlebars + Node.js

  handlebars.js, javascript, node.js, yelp-fusion-api

In my app.js I made this function that I took from yelp fusion

function getYelpData(cityN){
    let name = "";
    let phone = "";
    let title = "";

    const result = client.search({
        location: cityN,
        }).then(res => {
            name = res.jsonBody.businesses[0].name,
            phone = res.jsonBody.businesses[0].phone,
            title = res.jsonBody.businesses[0].categories[0].title
        }).catch(e => {
            console.log(e)
        });
    
    return result // returning me [object Promise]
}

and what I was trying to do was getting name, phone and title in order to send them to my public/js/fecthData.js

To achieve this my app.js has also this:

app.get('/shops?city=', (req, res) => {
    let cityN = req.city;
    let name = getYelpData(cityN) // just a test with one param
    console.log("Call: " + name)
    res.send({
        name,
    })
});

which by calling the getYelpData() func should send those 3 data to my public/js/fecthData.js.

I was trying to call it by /shops?city= so I can select the city that user insert in the form. Of course it doesn’t work. I’m not able to get data from the function and they never arrives to public/js/fetchData.js

In fetchData.js i fetch data like this :

const yelpApi = "/shops?city="

fetch(yelpApi)
.then(res => res.text())
.then(text => 
    document.getElementById('test').innerHTML = text
)
  1. How can I get those 3 values from getYelpData() and use cityN as parameter? (right now it returns me [Object Promise]
  2. Is /shops?city= correct to select the city that the user insert?

If I let getYelpData(cityN) return a string like "hello" it will be added correctly to my h3. The problem is getting the data correctly from the yelp fusion API call

enter image description here

Source: Ask Javascript Questions

LEAVE A COMMENT