1

I execute the following code;

 var array = await Promise.all([
    database.query("SELECT COUNT(amount) FROM transactions WHERE date >= now() - INTERVAL 1 DAY;"), 
    database.query("SELECT COUNT(amount) FROM transactions WHERE date >= now() - INTERVAL 7 DAY;"), 
    database.query("SELECT COUNT(amount) FROM transactions"), 
  ]);

I expect an array like so [1,2,3];

Instead what I get is;

[ [ RowDataPacket { 'COUNT(amount)': 6 } ],
  [ RowDataPacket { 'COUNT(amount)': 7 } ],
  [ RowDataPacket { 'COUNT(amount)': 7 } ] ]

What did I miss?

1 Answer 1

1

As per https://github.com/mysqljs/mysql/issues/1330, it is because it is a low level library.

You can still access the array with [0]['COUNT(amount)'] and so on. If you just want the numbers you may have too add more logic.

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

2 Comments

Shame. It really annoys me.
@Borda it shouldn't, because it takes 2 seconds to write a re-usable function that wraps query and returns a single value. If your library of choice would supply this feature it would also just be this exact same function. This is a learning opportunity.

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.