How do I get the results from my database query?

I have an express app which performs the following mariadb query:

// GET (init)
app.get('/init', async (req, res) => {
    try {
        const result = await db.pool.query("SELECT COUNT(DISTINCT (line)) FROM db.lines");
        console.log(JSON.stringify(result));
    } catch (err) {
        throw err;
    }
});

The result I get from this query is printed in console as:

[{"COUNT(DISTINCT (line))":8}]

This is great and it’s the correct data. My problem is I can’t figure out how the hell I access this data?!

I know if I had a query select book from bookcase where title = "A Game of Thrones" I could simply put in JSON.stringify(result.book) or result[0].book… but how does it work with a count distinct query?

Answer

You could do

result[0]["COUNT(DISTINCT (line))"]

You can also us AS keyword to name returned data e.g

SELECT COUNT(DISTINCT (line)) AS count FROM db.lines

and it would return you

[{"count":8}]

You could then use

result[0].count