node.js knex queries - running update after select

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












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((rating) =>
console.log(rating);
);

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)
.then(() => return 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;
);







share|improve this question



























    up vote
    0
    down vote

    favorite












    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((rating) =>
    console.log(rating);
    );

    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)
    .then(() => return 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;
    );







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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((rating) =>
      console.log(rating);
      );

      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)
      .then(() => return 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;
      );







      share|improve this question













      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((rating) =>
      console.log(rating);
      );

      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)
      .then(() => return 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;
      );









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 15 at 14:48









      200_success

      123k14143401




      123k14143401









      asked Jan 15 at 9:26









      renathy

      1012




      1012

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185126%2fnode-js-knex-queries-running-update-after-select%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185126%2fnode-js-knex-queries-running-update-after-select%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?