我有一个非常复杂的查询。在此之前还有其他一些问题,我不想关注,因为它有效。
我想要一个产品类别的所有产品,其中有一个索赔。从索赔开始。不可能做一个新的" .then()"因为我需要相应产品中的索赔ID。
感谢您的各种帮助。
这是代码:
.then(claims => {
const uniqClaims = _.uniqBy(claims, 'OnId');
uniqClaims.forEach(claim => {
CatProduct.findAll({
where: {
ProductCategoryId: claim.OnId,
},
}).then(catproducts => {
const ProductId = _.map(catproducts, product => product.ProductId);
Product.findAll({
where: {
id: ProductId,
},
}).then(newProducts => {
console.log('newProducts');
const addProducts = newProducts.map(v => v.ClaimId = claim);
products.push(...addProducts);
});
});
});
console.log(products);
return res.status(200).json(products);
});
在声明和产品之间没有直接的,但是传递关系。所以{where:ClaimId:claimIds}是不可能和无意的。 我想在newproduct-Object中拥有声明ID。有什么想法吗?
非常感谢! Laurens的
更新 我现在改为:
.then(uniqClaims => {
const claimIds = uniqClaims.map(claim => claim.OnId);
let products = [];
claimIds.foreach(claimId => {
CatProduct.findAll({
where: {
ProductCategoryId: claimId,
}
}).then(catproducts => {
const ProductIds = _.map(catproducts, product => product.ProductId);
Product.findAll({
where: {
id: ProductIds,
}, raw: true
}).then(newProducts => {
const serializedNewProducts = newProducts;
const newProductsWithClaims = serializedNewProducts.map(v => {
v.claim = claimId;
return v;
});
products.push(...newProductsWithClaims);
});
});
};
res.status(200).json(products);
});
"当然"它没有用,因为foreach不是异步的。我怎么能让它异步。我尝试了很多,包括Promise,async等的回顾,但它不起作用。有人可以在这里写出正确的代码。我真的被困在这里了。
答案 0 :(得分:0)
我试图简化你的代码但是没有尝试过。
.then(claims => {
// Array of unique claims
claims = _.uniqBy(claims, 'OnId');
// claimIds => Array of unique ids
var claimIds = claims.map(claim => claim.OnId);
// find all cat products that have one of claimIds
CatProduct.findAll({
where: {
// sequelize will use `in` operator automatically since claimIds is an array
ProductCategoryId: claimIds,
}
}).then(catproducts => {
// productIds => Array of ids
var productIds = catproducts.map(product => product.ProductId);
// find all products that have
// one of productIds
Product.findAll({
where: {
// sequelize will use `in` operator automatically since productIds is an array
id: productIds
},
}).then(newProducts => {
console.log('newProducts');
console.log(newProducts);
newProducts.forEach(function(product){
// find a ProductCategoryId
var categoryId = catproducts.find(function(catproduct){
if (catproduct.ProductId === product.id)
return catproduct.ProductCategoryId;
});
// find claim by ProductCategoryId
var claim = claims.find(c => c.OnId === categoryId);
product.claim = claim;
});
return res.status(200).json(newProducts);
});
});
});
// claimIds.foreach(claimId => {
CatProduct.findAll({
where: {
ProductCategoryId: claimIds, // find all products in a single query
}
}).then(catproducts => {
const ProductIds = catproducts.map(p => p.ProductId);
Product.findAll({
where: {
id: ProductIds,
},
raw: true
}).then(newProducts => {
const serializedNewProducts = newProducts;
const newProductsWithClaims = serializedNewProducts.map(v => {
//v.claim = claimId;
// each catproducts's ProductCategoryId is claimId
v.claim = catproducts.find(c => c.ProductId === v.id).ProductCategoryId;
return v;
});
products.push(...newProductsWithClaims);
});
});
// });