I am working on node.js project for two days. This is my first node.js code. All the time I am confused with knex and Promises.
First I didn’t add “returns” after knex queries. But then my code never executed knex query. SO, I added “returns’, but this is confusing for me, to have multiple returns in each function. Please, check my code.
update_product_rating(13) .then((affected) => { console.log(affected); }); function update_product_rating(product_id) { return calculate_product_rating(product_id) .then((rating) => { rating = rating == null ? 0 : rating; console.log('rating = '); console.log(rating); return knex('products') .where('id', product_id) .update('rating', rating); }) } function calculate_product_rating(product_id) { // sum up all ratings return knex('product_ingredients') .join('ingredients', 'ingredients.id', '=', 'product_ingredients.ingredient_id') .where("product_ingredients.product_id", product_id) .sum('ingredients.rating_adjustments as adjustment') .then(function (res) { return res.adjustment; }); }