I am trying to determine a better way for my query. I have two tables, one is for recipes, and another is for reviews. Currently, I have two separate queries to find the recipe, and then find the reviews associated with the recipe. Is there a better way I could be doing this?
RecipeController
module.exports = {
viewRecipe: function(req, res) {
var recipeId = parseInt(req.params.id, 10);
var recipeM = {};
var reviewM = {};
db.recipe.find({
where: {
id: recipeId
}
}).then(function(recipe) {
recipeM = recipe.dataValues;
recipeM.ingredients = recipe.ingredients.replace(/\s+/g, '').split(",");
recipeM.instructions = recipe.instructions.split("\n");
}, function (e) {
});
db.review.findAll({
where: {
recipeId: recipeId
}
}).then(function(review) {
console.log(review);
res.render('viewRecipe', { recipe: recipeM, review: review, categories: categoriesMain, title: recipeM.title });
}, function(e) {
});
},
Sequelize.Promisecould help you group these two parallel queries and wait for them to finish with your final query.