-1
I have 2 files in both async use / await and return a promisse.
So far so good.
But my question is what to call them in Controller.
I’m managing to return all recipes, however, I’m not able to add the parameter gif on the return of the object in the API.
I tried to pass await inside the property gif: await but I was unsuccessful.
const getRecipes = require('../services/recipePuppy');
const getGifFromRecipe = require('../services/giphy');
module.exports = {
async getRecipes(request, response) {
const clientRequest = request.query;
const ingredientsList = clientRequest.i.split(',');
const recipesResponse = await getRecipes.getRecipesFromIngredients(ingredientsList);
if (recipesResponse) {
response.json({
keywords: ingredientsList,
recipes: recipesResponse.results.map((recipe) => {
return {
title: recipe.title,
ingredients: recipe.ingredients,
link: recipe.href,
// gif: como obter o GIF?
}
})
});
}
}
}
Service code giphy:
const axios = require('axios');
module.exports = {
async getGif(search) {
const response = await axios.default.get(`https://api.giphy.com/v1/gifs/search?api_key=${process.env.GIPHY_API}&q=${search}&limit=1&offset=0&rating=g&lang=${process.env.GIPHY_LANGUAGE}`);
return response.data;
}
}