Hi,
I’ve just completed the Ravenous project and now I am practicing how to implement additional functionalities by myself.
I’m trying to add suggestions as they type something in the search bar. See below code.
Calling function suggest(text), eventhough I returned ‘suggestions’ always equals a promise so now I’m trying to push the results into the suggestions array instead.
But I still can’t get this to work. The console.log within the function itself gives me the right results which is in this case: [“Reservations”, “Ramen Delivery”, “Rooftop Bar”]
But the console.log outside is still giving me an empty array.
Can anyone help return the right value?
let text = "R";
let suggestions = [ ];
function suggest(text) {
fetch(
`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/autocomplete?text=${text}&limit=10`,
{ headers: { Authorization: `Bearer ${apiKey}` } }
)
.then(response => response.json())
.then(jsonResponse => {
if (jsonResponse.terms) {
const Arr = jsonResponse.terms.map(suggest => {
return {
text: suggest.text
};
});
Arr.forEach(obj => {
suggestions.push(`${obj.text}`);
});
}
console.log(suggestions);
return suggestions;
});
}
suggest(text);
console.log(suggestions);