复杂嵌套的sequelize循环查询,具有更高级别的值

时间:2018-05-23 08:07:11

标签: javascript node.js sequelize.js

我有一个非常复杂的查询。在此之前还有其他一些问题,我不想关注,因为它有效。

我想要一个产品类别的所有产品,其中有一个索赔。从索赔开始。不可能做一个新的" .then()"因为我需要相应产品中的索赔ID。

感谢您的各种帮助。

enter image description here

这是代码:

.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等的回顾,但它不起作用。有人可以在这里写出正确的代码。我真的被困在这里了。

1 个答案:

答案 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);
    });
});

// });