typeorm v0.2.22 skips some data to save data in mysql db when running in for loop - NodeJs

Viewed 23

here in for loop with other operations ie: searching product, i have added for of loop and mapping product with store in mapping table. data size is in thousands.

for (let store of storesList) {
  storeProductMappingRepository
    .findOne({
      relations: ["store", "product"],
      where: {
        store: store,
        product: product,
      },
    })
    .then((isMappingAvailable) => {
      if (!isMappingAvailable) {
        let storeProductMapping = new StoreProductMapping();
        storeProductMapping.price = item.Item_Price;
        storeProductMapping.store = store;
        storeProductMapping.product = product;
        storeProductMappingRepository.save(storeProductMapping);
      }
    });
}

1 Answers

You must correctly "await" each promise (async code), otherwise the function may (most of the time) return before processing all stores.

For more information, take a look at Promise from MDN.

Two solutions are available:

  1. await on each iteration
for (let store of storesList) {
    // await here
    const isMappingAvailable = (await storeProductMappingRepository
        .findOne({
            relations: ["store", "product"],
            where: {
                store: store,
                product: product,
            },
        })) !== undefined;

    if (!isMappingAvailable) {
        const storeProductMapping = new StoreProductMapping();
        storeProductMapping.price = item.Item_Price;
        storeProductMapping.store = store;
        storeProductMapping.product = product;
        // await here
        await storeProductMappingRepository.save(storeProductMapping);
    }
}

// Executed
  1. Single await with Promise.all(...)
    Requires a little bit more code but can run multiple Promise(s) in parallel.
// Create an array of Promise(s)
const promises: { (): Promise<void>} [] = [];

for (let store of storesList) {
    // Add a promise for each store
    promises.push(async () => {
        // await here
        const isMappingAvailable = (await storeProductMappingRepository
            .findOne({
                relations: ["store", "product"],
                where: {
                    store: store,
                    product: product,
                },
            })) !== undefined;

        if (!isMappingAvailable) {
            const storeProductMapping = new StoreProductMapping();
            storeProductMapping.price = item.Item_Price;
            storeProductMapping.store = store;
            storeProductMapping.product = product;
            // await here
            await storeProductMappingRepository.save(storeProductMapping);
        }

        return Promise.resolve();
    });
}

// Resolve/Execute all
await Promise.all(promises);

// Executed
Related