Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  134] [ 5]  / answers: 1 / hits: 45620  / 7 Years ago, thu, march 30, 2017, 12:00:00

I'm trying to create a array structure. So, we have parts and each part had articles.



So i did a promise to collect all parts .then() i need to iterate part promise and select the articles in this part .then() i want to push this to a array parts and render this in a view.



The structure is this:



-PARTS
- part
- u_order
- u_familia
- u_part
- u_type
- articles (article from each part)


And my code is this:



var p1 = new Promise(function(resolve, reject) {
var stamp = req.params.stamp;
request.query(SELECT u_order, u_familia, u_part, u_type FROM u_part (nolock) where u_order <'92' and u_order <> '100').then((data)=>resolve(data));
// or
// reject (Error!);
});



p1.then(function(value){
var stamp = req.params.stamp;
console.log(value.length);
for(var i= 0; i<value.length; i++)
{
console.log(value[i]);
request.query(SELECT st.u_posic, sc.ref, sc.qtt, sc.design FROM st INNER JOIN sc ON st.ref = sc.ref where sc.ststamp ='+stamp+' and st.u_posic = '+value[i].u_order+').then((data)=>resolve(data));
}

}, function(reason){
console.log(reason);
});

p1.then(function(part, articles){
var parts = [];
console.log(PART: +part.length);
for(var j= 0; j<part.length; j++)
{
console.log(part[j].u_order);
console.log(part[j].u_familia);
console.log(part[j].u_part);
console.log(part[j].u_type);
console.log(articles[j]);
};
});


In last .then() i just have the parts, i can't access to articles maybe because i'm not doing well with second .then()
I'm starting working with promises, i also read documentation but i can't do this.



Anyone can help me to understand and solve this?



Thank you


More From » node.js

 Answers
31

The best I can do - I'll add explanation a little later (sorry, have to go)



request.query(SELECT u_order, u_familia, u_part, u_type FROM u_part (nolock) where u_order <'92' and u_order <> '100')
.then(function(parts){
var stamp = req.params.stamp;
return Promise.all(parts.map(function(part) {
return request.query(SELECT st.u_posic, sc.ref, sc.qtt, sc.design FROM st INNER JOIN sc ON st.ref = sc.ref where sc.ststamp ='+stamp+' and st.u_posic = '+part.u_order+')
.then(function(articles) {
part.articles = articles;
return part;
});
}));
})
.then(function(parts){
parts.forEach(function(part) {
console.log(part.u_order);
console.log(part.u_familia);
console.log(part.u_part);
console.log(part.u_type);
part.articles.forEach(function(article) {
console.log(article.u_posic);
console.log(article.ref);
console.log(article.qtt);
console.log(article.design);
});
});
});



BONUS ES2015+ version of above




request.query(SELECT u_order, u_familia, u_part, u_type FROM u_part (nolock) where u_order <'92' and u_order <> '100')
.then(parts => Promise.all(parts.map(part => request.query(`SELECT st.u_posic, sc.ref, sc.qtt, sc.design FROM st INNER JOIN sc ON st.ref = sc.ref where sc.ststamp ='${req.params.stamp}' and st.u_posic = '${part.u_order}'`)
.then(articles => {
part.articles = articles;
return part;
})
)))
.then(parts => parts.forEach(part => {
console.log(part.u_order);
console.log(part.u_familia);
console.log(part.u_part);
console.log(part.u_type);
part.articles.forEach(article => {
console.log(article.u_posic);
console.log(article.ref);
console.log(article.qtt);
console.log(article.design);
});
}));

[#58331] Tuesday, March 28, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
travion

Total Points: 137
Total Questions: 96
Total Answers: 103

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
travion questions
Mon, Dec 16, 19, 00:00, 5 Years ago
Sat, Oct 19, 19, 00:00, 5 Years ago
Fri, Sep 20, 19, 00:00, 5 Years ago
Wed, Nov 14, 18, 00:00, 6 Years ago
Sun, Oct 28, 18, 00:00, 6 Years ago
;