【问题标题】:Complex nested sequelize loop query with value of higher level具有更高级别值的复杂嵌套 sequelize 循环查询
【发布时间】:2018-05-23 08:07:11
【问题描述】:

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

我想要一个 Productcategory 的所有产品,其中有一个声明。从索赔开始。无法创建新的“.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。有什么想法吗?

非常感谢! 劳伦斯

更新 我现在将其更改为以下内容:

.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 不是异步的。我怎样才能使它异步。我尝试了很多,包括回顾 Promises、异步等,但它只是不起作用。有人可以在这里写下正确的代码吗?我真的被困在这里了。

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    我试图简化你的代码,但还没有尝试过。

    .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);
        });
    });
    
    // });
    

    【讨论】:

    • 非常感谢!我需要在 newProducts 对象中:newProducts.claim = claimId。相应的声明 ID 在此块中不可用。这就是为什么我尝试迭代,但它不起作用
    • 而且claim和product之间没有直接的,而是传递关系,所以{where: ClaimId: claimIds}是不可能的。有什么想法吗?
    • 我已经更新了代码看看。我完全错过了你在v => v.ClaimId = claim 中分配的内容,我认为这是比较。
    • 嗨 Molda,我终于让它工作了(请参阅我的新更新),但现在我遇到了同步 forEach 的问题。你能帮我么? :)
    • 爱你摩尔达:D
    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2020-11-15
    • 2016-04-12
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多