-1

I am new to the asynchronous and web application development and i cannot figure out a way to return data from a function before my page is rendered. Also new to sequelize and any feedback on formatting or best practices is appreciated.

I have tried setting this function as a route which in fact does return data if i send it as a response res.send(recipes). But i want it to act as a function that i can call before my page gets rendered

getRecipes.js

const sequelized = require('sequelize'); 
const op = sequelized.Op;

async function getRecipes(){
    //SELECT * FROM ingredients
    ingredients.findAll({ 
        where: {}, 
        raw : true 
    }) 
    .then(ingredients_result =>{ 
        //Get ingredient that expires soon
        //Find recipes of the ingredient that expires soon
        recipe_ingredient.findAll({ 
            where: {}, 
            raw: true 
        }) 
        .then(recipe_ingrdient_result =>{ 
            //If we have all ingredients for a recipe then find name of that recipe by ID
            recipes.findAll({ 
                where: {recipe_id: {[op.in]: suggested_recipes}} 
            }) 
            .then(recipes =>{
                someinfo = JSON.stringify(recipes);
                // This is where i need the date to be returned from
                return someinfo; // But every time i load a page this returns undefined
            }) 
        })
    })
}

module.exports.getRecipes = getRecipes;

routes/user.js

const getStuff = require('./getRecipes');
router.get('/dashboard', async function(req, res){
    //This returns undefined
    var r_name = await getStuff.getRecipes();
    res.render('dashboard',{
            title:"Dashboard",
        });
    })

I am probably misunderstanding how async works, so any help would be appreciated! I know want to be able to retrieve results by running getRecipes function before the page gets rendered.

  • `getRecipes` isn't returning anything. Change to `return ingredients.findAll...` and also `return recipe_ingredient.findAll...` (and try to avoid the promise-as-callback antipattern) – CertainPerformance Feb 04 '19 at 05:42
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – adiga Feb 04 '19 at 05:42
  • This is not a dupe. *I am probably misunderstanding how async works* - you have problems with promises first of all. You don't chain promises properly. Always return from `then`. Don't nest `then` unless this is necessary. You also don't handle errors in a middleware. – Estus Flask Feb 04 '19 at 07:25
  • I understand this might be simple for some of you but switching from synchronous to non synchronous is kind of not easy, yet thank you for the feedback. – Oskar Dauksts Feb 04 '19 at 14:13

1 Answers1

1

First of all , if you have made the function the async then use it ( DO READ ):

This is how you should use async/await in your code :

async function getRecipes() {
    //SELECT * FROM ingredients
    let ingredients_result = await ingredients.findAll({ // <------- CHANGE IS HERE
        where: {},
        raw: true
    });

    //Get ingredient that expires soon
    //Find recipes of the ingredient that expires soon
    let recipe_ingrdient_result = await recipe_ingredient.findAll({ // <------- CHANGE IS HERE
        where: {},
        raw: true
    });

    //If we have all ingredients for a recipe then find name of that recipe by ID
    let recipes_result = await recipes.findAll({ // <------- CHANGE IS HERE
        where: {
            recipe_id: {
                [op.in]: suggested_recipes
            }
        }
    })

    let someinfo = JSON.stringify(recipes_result);
    return someinfo; 
}
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122