1

I am currently trying to build a dashboard on my Node.js project. To get an overview of certain values, they should be summed up. My SELECT works fine in my MySQL database, but if I include it in my Node project then I will not get any output.

app.get('/dash', function(req, res, next) {
    connection.query("SELECT SUM(g_speicherplatz), SUM(g_spielzeit), SUM(g_kosten) FROM games", function(error, result, fields) {
        if(error) {
            req.flash('error', error)
            res.render('games/g-dash', {
                data: ''
            })
        } else {
            res.render('games/g-dash', {
                data: result,
            })
        }
    })
})

EJS Output:

<div class="container">
    <div class="row">
        <% if (data) { 
            console.log("Anzahl Datensätze: " + data.length);
            data.forEach(function(dash){ %>
            <div class="card mb-3">
               <h3 class="card-header">Statistiken</h3>
               <h3><%= dash.g_speicherplatz %></h3>
               <h3><%= dash.g_spielzeit %></h3>
               <h3><%= dash.g_kosten %></h3>
            </div>
        <% }) %>
        <% } %>
    </div>
</div>

Where is my mistake?

2 Answers 2

2

You need to assign aliases to the sums.

    connection.query("SELECT SUM(g_speicherplatz) AS g_speicherplatz, SUM(g_spielzeit) AS g_spielzeit, SUM(g_kosten) AS g_kosten FROM games", function(error, result, fields) {

Sign up to request clarification or add additional context in comments.

Comments

1

Try adding aliases to your query, and then refer to them in your view:

SELECT
    SUM(g_speicherplatz) AS g_speicherplatz_sum,
    SUM(g_spielzeit) AS g_spielzeit_sum,
    SUM(g_kosten) QS g_kosten_sum
FROM games;

And in the view:

<div class="card mb-3">
    <h3 class="card-header">Statistiken</h3>
    <h3><%= dash.g_speicherplatz_sum %></h3>
    <h3><%= dash.g_spielzeit_sum %></h3>
    <h3><%= dash.g_kosten_sum %></h3>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.