I need to create an array with this structure:
[
{
position: 2,
family: 9404,
part: [ 'article1', 'article2', 'article3' ]
},
{
position: 3,
family: 9405,
part: [ 'article4', 'article5', 'article6' ]
}
]
So i have a form where i select the parts that i want and send the families to get url.In the getter function i do a for to get the articles of each family and i want to query a select of articles and a select of positions. After that i try to push each array to a main array but i can't, show me undefined. How can i do this kind of operations?
I'm new with node and express and this is the first time that i have to do that.
My code:
getFamilies(req, res)
{
console.log(req.params.data);
var parsedData = JSON.parse(req.params.data);
var compounds = parsedData[0].compounds;
var supplier = parsedData[0].supplier;
var families = parsedData[0].families;
console.log(parsedData[0].compounds.length);
var position = [];
var data = [];
var parts = [];
for (var i = 0; i < compounds.length; i++)
{
parts.push(request.query("SELECT st.ref, st.design FROM st WHERE familia ='"+families[i]+"'"));
position.push(request.query("SELECT u_order FROM u_part WHERE u_familia='"+families[i]+"'"));
}
return Promise.all(parts, position, families).then(function(listOfResults)
{
//add parts, position and families to data[]
var data = [];
//console.log(data);
console.log(listOfResults);
console.log("done");
//return listOfResults;
res.render('view2', {teste: data});
}).catch(function(err)
{
// ... query error checks
console.log(err);
});
}
In promise just print the first parameter "parts" and if i put the [parts, position, families]
give me promise pending. And how can i put the data in the structure that i show above.
parseData:
[
{
"compounds": ["8"],
"supplier": ["sup"],
"families": ["9305"]
}
]
Please teach me how can i do this kind of operations.
Thank you
You incorrectly use Promise.all
, it takes array of promises
return Promise.all([Promise.all(parts), Promise.all(position)]).then(function(listOfResults){
var partResult = listOfResults[0];
var positionResult = listOfResults[1];
var data = [];
for (var i=0; i<families.length; i++) {
var family = families[i];
var pos = positionResult[i];
var parts = partResult; // logic to extract parts for current family
data.push({family: family, position: pos, parts: parts})
}
console.log(data);
})
families
to Promise.all
families
seems to just be an array of data from taken from the query Promise.all
takes an array of promises in input, and you're passing an array of arrays of promises and of data... So do:
Promise.all([...parts, ...position])
or if you're not using ES6 syntax Promise.all(parts.concat(position))
and fix your SQL!
====
Final code could look like:
getFamilies = (req, res) => {
var families = JSON.parse(req.params.data)[0].families;
var positions = [];
var data = [];
var parts = [];
families.forEach(family => {
// see http://stackoverflow.com/a/7760578/2054629 for mysql_real_escape_string
parts.push(request.query("SELECT st.ref, st.design FROM st WHERE familia ='"+mysql_real_escape_string(family)+"'"));
positions.push(request.query("SELECT u_order FROM u_part WHERE u_familia='"+mysql_real_escape_string(family)+"'"));
});
return Promise.all([Promise.all(parts), Promise.all(positions)]).then(listOfResults => {
var [partResult, positionResult] = listOfResults;
var data = families.map((family, i) => {
var pos = positionResult[i];
var parts = partResult[i];
return {family: family, position: pos, parts: parts};
});
res.render('view2', {teste: data});
}).catch(err => {
// ... query error checks
console.log(err);
});
};
Promise.all()
接受一个promise数组,好像您正在传递多个数组。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.