You can also use array destructuring to unpack the first row from the results parameter:
const sql = "SELECT age FROM `account` WHERE `name` = ? order by date desc limit 1";
connection.query({ sql, values: [market.name] }, function (error, [account], fields) {
market.fee = account.age;
resolve(market);
});
Or go wild and throw some object destructuring into the mix:
const sql = "SELECT age FROM `account` WHERE `name` = ? order by date desc limit 1";
connection.query({ sql, values: [market.name] }, function (error, [{ age }], fields) {
market.fee = age;
resolve(market);
});
Another refactor, does the same thing but uses a concise body arrow function and spreads market
into a new object which has fee
, mapped in the input parameters [{ age: fee }]
.
connection.query(
{
sql: "SELECT age FROM `account` where `name` = ? order by date desc limit 1",
values: [market.name]
},
(error, [{ age: fee }], fields) => resolve({ ...market, fee })
);