results.length is not give the correct output for MySQL query?

ismsm

Why the length of result of MySQL query is not correct? The query is just count the number of message column in database and print the length.

the code:

connection.query("SELECT COUNT(message) as total FROM `counter`", (err, results) => {
    if(err) throw err;
    console.log(results);
    console.log("the length of result:", results.length);

the output:

[ RowDataPacket { total: 4 } ]
the length of result: 1

The correct length is 4 not 1.

How to correct that, please?

cbr

results contains the rows returned as an array. Since you're using SELECT COUNT(message), you're querying for the count of messages, which returns one row. This one row contains the result, which is an object of shape { total: 4 } - where the total comes from the as total part of your SQL query.

To get the actual result, check that results.length > 0 and then you can access results[0].total:

if (results.length > 0) {
  console.log('Total:', results[0].total)
} else {
  throw new Error('No results returned from query!')
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not retrieving correct results with mysql query using parentheses

From Dev

Not retrieving correct results with mysql query using parentheses

From Java

How to output MySQL query results in CSV format?

From Dev

SQL query output is correct, but the program in php is written is unable to show the results

From Dev

comparing two objects does not give correct results

From Dev

How to send MySQL query results to outfile and standard output at the same time?

From Dev

How to output MySQL query results in csv format in Windows environment?

From Dev

How to send MySQL query results to outfile and standard output at the same time?

From Dev

How to output MySQL query results in csv format in Windows environment?

From Dev

Why does this insertionSort code not give the correct output?

From Dev

Multiple AND/OR Query Not Yielding Correct Results

From Dev

sql query not returning correct results

From Dev

SPARQL query doesn't give any results

From Dev

Elasticsearch term query does not give any results

From Dev

Why is this MySQL query correct?

From Dev

MySQL Query not correct

From Dev

Versioning for MySQL Query Results

From Dev

MySQL fallback query if no results

From Dev

Inaccurate results with MySql query

From Dev

MySQL query fliltering results

From Dev

MySQL query filtering results

From Dev

Utilizing MySQL query results

From Dev

Array with MySQL query results

From Dev

Transpose the results of a MySQL query

From Dev

Inaccurate results with MySql query

From Dev

Mysql query returns no results

From Dev

mysql query to fetch these results

From Dev

Elasticsearch search Java API doesn't give correct results

From Dev

Different SPARQL query engines give differing results for DESCRIBE Query

Related Related

  1. 1

    Not retrieving correct results with mysql query using parentheses

  2. 2

    Not retrieving correct results with mysql query using parentheses

  3. 3

    How to output MySQL query results in CSV format?

  4. 4

    SQL query output is correct, but the program in php is written is unable to show the results

  5. 5

    comparing two objects does not give correct results

  6. 6

    How to send MySQL query results to outfile and standard output at the same time?

  7. 7

    How to output MySQL query results in csv format in Windows environment?

  8. 8

    How to send MySQL query results to outfile and standard output at the same time?

  9. 9

    How to output MySQL query results in csv format in Windows environment?

  10. 10

    Why does this insertionSort code not give the correct output?

  11. 11

    Multiple AND/OR Query Not Yielding Correct Results

  12. 12

    sql query not returning correct results

  13. 13

    SPARQL query doesn't give any results

  14. 14

    Elasticsearch term query does not give any results

  15. 15

    Why is this MySQL query correct?

  16. 16

    MySQL Query not correct

  17. 17

    Versioning for MySQL Query Results

  18. 18

    MySQL fallback query if no results

  19. 19

    Inaccurate results with MySql query

  20. 20

    MySQL query fliltering results

  21. 21

    MySQL query filtering results

  22. 22

    Utilizing MySQL query results

  23. 23

    Array with MySQL query results

  24. 24

    Transpose the results of a MySQL query

  25. 25

    Inaccurate results with MySql query

  26. 26

    Mysql query returns no results

  27. 27

    mysql query to fetch these results

  28. 28

    Elasticsearch search Java API doesn't give correct results

  29. 29

    Different SPARQL query engines give differing results for DESCRIBE Query

HotTag

Archive