【发布时间】:2020-05-22 00:58:32
【问题描述】:
早安,
您能否建议如何为每个类别创建仅属于该类别的项目列表,如下所示:
[
{
"id": category1,
"name": "Category1",
"products": [
{
"id": product1,
"name": product1.name
"images": [
{
"id": product1,
"url": product1.img,
},
]
},
{
"id": category2,
"name": "Category2",
"products": [
{
"id": product2,
"name": product.name
"images": [
{
"id": product2,
"url": product2.img,
}]
},
{
"id": product3,
"name": product3.name
"images": [
{
"id": product3,
"url": product3.img,
}]
}
]
}
]
这是对数据库的请求:
connection.query("SELECT productId, productName, productImg, productCategoryId, ProductCategoryName FROM Items", function (err, tmpres) { }
if (err) {
console.log("query failed!" + err);
return res.json(500, err);
}
var data = [];
tmpres.forEach(function (a) {
data.push({
id: a.productCategoryId,
title: a.productName
....
});
res.json(data);
提前致谢!
【问题讨论】:
-
您有语法错误。你立即关闭你的回调函数
function (err, tmpres) { }
标签: javascript mysql node.js api