[英]Access a nested array of objects in nodejs, mysql and json
I have these tables:我有这些表:
CREATE TABLE Progress_Category (
categoryId int(4) AUTO_INCREMENT NOT NULL,
name varchar(150) NOT NULL,
PRIMARY KEY (categoryId)
);
CREATE TABLE Progress_Skill (
skillId int(4) AUTO_INCREMENT NOT NULL,
name varchar(150) NOT NULL,
currentProgress int NOT NULL,
`25` varchar(300) NOT NULL,
`50` varchar(300) NOT NULL,
`75` varchar(300) NOT NULL,
`100` varchar(300) NOT NULL,
categoryId int(4) NOT NULL,
PRIMARY KEY (skillId),
CONSTRAINT Constr_Progress_Skill_Skill_fk FOREIGN KEY Skill_fk (categoryId) REFERENCES Progress_Category(categoryId) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE Progress_Message (
messageId int(4) AUTO_INCREMENT NOT NULL,
message varchar(500) NOT NULL,
messageDate DATE NOT NULL,
skillId int(4) NOT NULL,
PRIMARY KEY (messageId),
CONSTRAINT Constr_Progress_Message_Message_fk FOREIGN KEY Message_fk (skillId) REFERENCES Progress_Skill(skillId) ON DELETE CASCADE ON UPDATE CASCADE
);
I have this query to retrieve all the data in a table:我有这个查询来检索表中的所有数据:
SELECT *
FROM Progress_Category AS pcat
LEFT JOIN Progress_Skill AS ps
ON pcat.categoryId = ps.catParentId
LEFT JOIN Progress_Message AS pm
ON ps.skillId = pm.skillParentId
For each skill of a category a new row of category will be created, with the respected skill.对于一个类别的每个技能,将使用受尊重的技能创建一个新的类别行。 For each message of a skill a new row with the category and the skill will be created, with the respected message.
对于技能的每条消息,将创建一个带有类别和技能的新行,以及受尊重的消息。
Query result:查询结果:
+------------+-----------+---------+-----------+-----------------+------+-------+--------+-------+-------------+-----------+-------------------------+-------------+---------------+
| categoryId | catname | skillId | skillname | currentProgress | 25 | 50 | 75 | 100 | catParentId | messageId | message | messageDate | skillParentId |
+------------+-----------+---------+-----------+-----------------+------+-------+--------+-------+-------------+-----------+-------------------------+-------------+---------------+
| 1 | Languages | 1 | Spanish | 100 | Read | Write | Listen | Speak | 1 | 1 | Native language | 2022-08-27 | 1 |
| 1 | Languages | 2 | English | 85 | Read | Write | Listen | Speak | 1 | 2 | Learning since 2016 | 2022-08-27 | 2 |
| 1 | Languages | 2 | English | 85 | Read | Write | Listen | Speak | 1 | 3 | Can speak almost fluent | 2022-08-27 | 2 |
| 2 | Projects | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | Ideas | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+------------+-----------+---------+-----------+-----------------+------+-------+--------+-------+-------------+-----------+-------------------------+-------------+---------------+
5 rows in set (0.001 sec)
In nodejs I use that query and the following code:在 nodejs 中,我使用该查询和以下代码:
connection.query(myquery, function(err, results, fields) {
if (err) {
console.log('----> Error with MySQL query in /api/showProgress: ' + err.message);
}
else{
console.log('Query successful, results are being displayed.');
var categories = [];
for (let category in results) {
if(categories.length > 0){
for(let key in categories){
if(results[category].categoryId !== categories[key].Category.Id){
console.log("Category Id: " + results[category].categoryId + " Id already in the array: " + categories[key].Category.Id);
categories.push({
"Category" : [{
"Id" : results[category].categoryId,
"Name" : results[category].catname
}]
});
}
}
}
}
else{
categories.push({
"Category" : [{
"Id" : results[category].categoryId,
"Name" : results[category].catname
}]
})
}
}
response.send({"My progress" : categories});
});
The result I get:我得到的结果:
Query successful, results are being displayed.
Category Id: 1 Id already in the array: undefined
Category Id: 1 Id already in the array: undefined
Category Id: 1 Id already in the array: undefined
Category Id: 2 Id already in the array: undefined
Category Id: 2 Id already in the array: undefined
Category Id: 2 Id already in the array: undefined
Category Id: 2 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
Category Id: 3 Id already in the array: undefined
So the problem is categories[key].Category.Id
.所以问题是
categories[key].Category.Id
。 I don't know how to access the property Id
that belong to Category
that is in the array.我不知道如何访问数组中属于
Category
的属性Id
。
The final idea is use that if so only one category is shown with an array of skills instead of a category, skill, same category, other skill:最后的想法是使用如果是这样,则只显示一个类别和一系列技能,而不是类别、技能、相同类别、其他技能:
Current:当前的:
{"My progress":[
{
"Category":[{
"Id":1,
"Name":"Languages",
"Skill":"asd"
}]
},
{
"Category":[{
"Id":1,
"Name":"Languages",
"Skill":"fgh"
}]
},
{
"Category":[{
"Id":1,
"Name":"Languages",
"Skill":"ijk"
}]
},
]}
Expected:预期的:
{"My progress":[
{
"Category":[
{
"Id":1,
"Name":"Languages",
"Skills":[{
"Name":"asd",
"Name":"fgh",
"Name":"ijk"
}]
},
{
"Id":2,
"Name":"Projects",
"Skills":[{
"Name":"123",
"Name":"456",
"Name":"789"
}]
}
]
}
]}
Got the expected result, changing almost everything:得到了预期的结果,几乎改变了一切:
{"My skills":[
{
"categoryId":1,
"CategoryName":"Web development",
"Subcategories":[
{
"parentId":1,
"subcategoryId":1,
"SubcategoryName":"Frontend",
"Skills":[
"Sass",
"Css",
"Bootstrap",
"Figma"
]
},
{
"parentId":1,
"subcategoryId":2,
"SubcategoryName":"Backend",
"Skills":[
"Nodejs",
"Express",
"MySQL",
"PHP"
]
}
]
},
{
"categoryId":2,
"CategoryName":"Cybersecurity",
"Subcategories":[
{
"parentId":2,
"subcategoryId":3,
"SubcategoryName":"Red team",
"Skills":[
"curl",
"Sherlock",
"Wappalyzer",
"Burpsuite"
]
},
{
"parentId":2,
"subcategoryId":4,
"SubcategoryName":"Blue team",
"Skills":[
"Cloudfare"
]
}
]
}
]}
Nodejs code:节点代码:
connection.query(myquery, function(err, results, fields) {
if (err) {
console.log('----> Error with MySQL query in /api/showSkills: ' + err.message);
}
else{
console.log('Query successful, results are being displayed.');
var mylist = [];
var subcat = [];
for (let key in results){
var currCatId = results[key].categoryId;
if(key > 0){
var lastKey = key - 1;
var backCatId = results[lastKey].categoryId;
if(currCatId != backCatId){ // new category
subcat.push({
'parentId': results[key].subcatParentId,
'subcategoryId': results[key].subcategoryId,
'SubcategoryName': results[key].subcatname,
'Skills': results[key].skills.split(',')
});
}
else{ // category is already in the list
subcat.push({
'parentId': results[key].subcatParentId,
'subcategoryId': results[key].subcategoryId,
'SubcategoryName': results[key].subcatname,
'Skills': results[key].skills.split(',')
});
var asd = 0;
for(let test in subcat){
if(subcat[test].parentId === results[key].categoryId && asd === 0){
mylist.push({
'categoryId': results[key].categoryId,
'CategoryName': results[key].catname,
'Subcategories': subcat
});
asd++;
}
}
subcat = [];
}
}
else{ // first register add it
subcat.push({
'parentId': results[key].subcatParentId,
'subcategoryId': results[key].subcategoryId,
'SubcategoryName': results[key].subcatname,
'Skills': results[key].skills.split(',')
});
}
}
response.send({"My skills" : mylist});
}
});
I'm pretty sure this will not work for more than 2 subcategories.我很确定这不适用于超过 2 个子类别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.