首页 > 解决方案 > my for is running twice when it was supposed to run only once

问题描述

I have a for where there is a request to upload an image. It should run only ONE time, but it is running twice and thus uploading the same image twice. I can't find the error, in my view it has the correct parameters

function uploadImage(itemsUpload) {
    requestImage
      .get(`images/url`)
      .json()
      .then(response => {

        let number= 1;

        for (let i = 0; i < number; i++) {

          if (itemsUpload.length > 0) {
            const URL = response.url
            const newURL = URL.replace('https://store-tst-marketplace.s3.amazonaws.com/', '')
            const img = itemsUpload[i]["file"]
            let blob = new Blob([img], { type: 'image/png' })

            request
              .put(`images/upload/${newURL}`, { body: blob, headers: { "content-type": blob.type } })
              .then(response => {
                console.log(response)
              })
          }
        }
      });
  }

标签: javascript

解决方案


推荐阅读