Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  71] [ 1]  / answers: 1 / hits: 18911  / 9 Years ago, wed, july 15, 2015, 12:00:00

Im using node and npm mysql to do some database work.



Is there any way to avoid using the result[0] , if i know Im only going to receive a single row?



connection.query({
sql: SELECT spend FROM `account` WHERE `name` = ? order by date desc limit 1,
values: [market.name]
}, function (error, results, fields) {
market.fee = results[0].age;
resolve(market);
});

More From » mysql

 Answers
70

The callback function of connection.query returns three values out of which the second one, is the resultset of query and hence an array of values.



Therefore, you must use result[0] even if you are sure that the resultset would contain just a single record.



Moreover, its somewhat query's own specification that decides what kind of data is supposed to be returned. mysql's SELECT is made to work this way(though you can limit the records) unlike mongodb's db.collection.findOne() where the query itself know it'll always return a single record


[#65804] Monday, July 13, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
heidys

Total Points: 665
Total Questions: 102
Total Answers: 97

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;