Gabriel
Gabriel

Reputation: 257

JSON result is showing command of SUM MySQL

I'm getting an effective return calculation of all values column my MYSQL table but when i went to check my api result; the column value sum command is appearing in the result of the JSON.

   [
      {
        "SUM(producFinalPrice)": 6000
      }
    ]

This my router get data from my data base.

router.get('/sell/home-card-last-sell-compared-to-the-previous-day', function (req, res) {
  connection.query('SELECT SUM(producFinalPrice) FROM new_sell',
    function (err, lastSellComparedToThePreviousDayCardHome, fields) {
      if (err) {
        throw err;
      }
      res.send(lastSellComparedToThePreviousDayCardHome);
    }
  );
});

`

Making my mistake clearer, i'm making the final request from the previous query in AJAX; Through this way.

$(function () {
  $.ajax({
    type: "GET",
    url: "/sell/home-card-last-sell-compared-to-the-previous-day",
    success: function (lastSellComparedToThePreviousDayCardHome) {
      var $lastSellComparedPreviousDay = $('#last-sell-compared-to-the-previous-day');
      $.each(lastSellComparedToThePreviousDayCardHome, function (i, SellPreviousDay) {
        $lastSellComparedPreviousDay.append('<li>R$ ' + SellPreviousDay.producFinalPrice + '.</li>');
      });
      console.log('Sucess return to Last Sell Previous Day!', lastSellComparedToThePreviousDayCardHome);
    }
  });
});

Basically that's it I could not find anything that would help me.. Thanks for helping me out ;]

Upvotes: 1

Views: 83

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522797

Use an alias:

connection.query('SELECT SUM(producFinalPrice) AS productFinalSum FROM new_sell', ...
                                               ^^^^^^^^

Then when you parse your JSON in Node check for the productFinalSum key. Actually, I think you could even use SUM(productFinalSum) to access your JSON, but it looks awkward.

Upvotes: 1

Related Questions