1
all right. Someone can help me with a matter of Angular, or the API with Node.js?
I consult Mysql in an api with Node.js with Inner Join in 2 tables, but how to expect mysql returns me everything in a single array, but I think it is more correct and feasible to return the arrays in level.
Example returning the Results directly from mysql:
[
{
coluna1: 'AAAAA',
coluna2: 'XXXXX'
},{
coluna1: 'AAAAA',
coluna2: 'YYYYY'
},{
coluna1: 'BBBBB',
coluna2: 'ZZZZZ'
},
]
What I want is to turn into that:
[
{
coluna1: 'AAAAA',
outra_arary: [
{
coluna2: 'XXXXX'
},{
coluna2: 'YYYYY'
}
]
},{
coluna1: 'BBBBB',
outra_arary: [
{
coluna2: 'ZZZZZ'
}
]
}
]
An example of how the method is:
router.get('/', (req, res, next) => {
res.locals.connection.query(`select *
from tabela1
inner join tabela2
on tabela1.id = tabela2.id;`, (error, results, fields) => {
if (error) {
res.send({
"status" : 500,
"error" : error,
"response" : null
});
} else {
res.send({
"status" : 200,
"error" : null,
"response" : results
});
}
});
});
Thank you João Pedro, I didn’t really understand the idea but I will try to do the test and put the answer here. I am new with Node.js. To this day I still pick up a little to understand these concepts.
– Fernando Silva Maransatto
I got John Peter, I understood the logic too, it was super simple. Very good, thank you.
– Fernando Silva Maransatto
John, now when I run it 2 times in a row, it returns my null array, as if I were storing cache, sometimes returns 304
– Fernando Silva Maransatto
The code or the request?
– João Pedro Henrique
It was the request that I was doing in the API, but then I discovered that there was something missing in that last line where we removed the duplicates, I realized that comparing the difference of your code here with the link you mentioned Fixed looks like this:
retornoFinal = retornoFinal.filter(function (a) {
 return !this[JSON.stringify(a)] && (this[JSON.stringify(a)] = true);
 }, Object.create(null));
Now it works!! Thank you very much!– Fernando Silva Maransatto
Well noted. I’m glad it all worked out ;D
– João Pedro Henrique