我正在尝试创建一个数组结构。所以,我们有零件,每个零件都有文章。
所以我承诺收集所有部分.then()
我需要迭代部分承诺并选择此部分中的文章.then()
我想将其推送到数组部分并在视图中呈现
结构如下:
-PARTS
- part
- u_order
- u_familia
- u_part
- u_type
- articles (article from each part)
我的代码就是这样:
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]);
};
});
在上一篇.then()
我只是有部分内容,我无法访问文章可能是因为我对第二部.then()
做得不好
我开始使用promises,我也阅读文档,但我不能这样做。
任何人都可以帮助我理解和解决这个问题吗?
谢谢
答案 0 :(得分:3)
我能做的最好 - 我稍后会补充说明(抱歉,不得不去)
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 +以上版本
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);
});
}));
答案 1 :(得分:1)
你弄错了。
第一个promise.prototype。然后只用一个Argument解析。请查看docs
第二件事承诺会返回一个承诺,所以你只是调用一个只在那里解析的查询,所以第一个解析数据没有变化,而链接它只会传递第一个数据,因为它是
< / LI>A().then((dt) => new Promise((resolve) => {
B().then((bData) => {
resolve({
A:dt,
B:bData,
})
})
})).then((combined) => {
console.log('%j', combined);
})
Output: {"A":[1,2,3],"B":[4,5,6]}
输出:{&#34; A&#34;:[1,2,3],&#34; B&#34;:[4,5,6]}
{{1}}