[英]Get data from MySQL database and add to list (NodeJS)
So using a list of ids, I'm trying to get data for each element in the database and add that element to a list. 因此,使用ID列表,我试图获取数据库中每个元素的数据并将该元素添加到列表中。 At the end, I'm displaying it on an HTML page.
最后,我将其显示在HTML页面上。 How would I do that?
我该怎么做? Right now, since it executes the queries async, the list will be empty after running the code to execute the query and add the result to a list.
现在,由于它异步执行查询,因此在运行代码以执行查询并将结果添加到列表之后,该列表将为空。
Lets assume you have a table called teams
that contains a row for each team, with columns like id
, games
, total_points
, wins
, and losses
. 让我们假设你有一个表叫做
teams
包含每个团队一行,像列id
, games
, total_points
, wins
和losses
。
Lets assume you have an array of team id
. 假设您有一组team
id
。 You want to query the database for those records, then do some things with them before returning them to the front end as json. 您想在数据库中查询这些记录,然后对它们执行一些操作,然后再将它们作为json返回前端。
This is how i understand your problem. 这就是我理解您的问题的方式。 If this isn't the case, please clarify in a comment on this answer.
如果不是这种情况,请在对此答案的评论中进行澄清。
Possible solution: Do it all in one query. 可能的解决方案:在一个查询中全部完成。 I'm going to use my favorite mysql library, knexjs .
我将使用我最喜欢的mysql库knexjs 。
var columns = ['id', 'games', 'total_points', 'wins', 'loses'];
function getTeams(ids){
return knex.select(columns).where('id', 'in', ids)
.then((teams) => {
//do whatever with array of teams
return teams;
}
}
It's pretty much as simple as that. 就这么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.