1

I'm a bit confused about some expected behavior regarding NodeLists, and was hoping that someone could point me in the right direction.

So I have a hidden input element whose value attribute holds a json string with a bunch of s3 upload data. It looks like this:

<input 
  name="uppyResult"
  type="hidden"
  value="[
    {
        &quot;successful&quot;:[
            {
            &quot;source&quot;:&quot;Dashboard&quot;,
            &quot;id&quot;:&quot;uppy-screen/shot/2021/07/15/at/4/00/02/pm/png-10-10-1d-1d-10-10-1e-1e-10-1e-image/png-165186-1626390007842&quot;,
            &quot;name&quot;:&quot;Screen Shot 2021-07-15 at 4.00.02 PM.png&quot;,
            &quot;extension&quot;:&quot;png&quot;,
            &quot;meta&quot;:{
                &quot;relativePath&quot;:null,
                &quot;name&quot;:&quot;Screen Shot 2021-07-15 at 4.00.02 PM.png&quot;,
                &quot;type&quot;:&quot;image/png&quot;,
                &quot;key&quot;:&quot;cache/4870f1e1d9f075c7397b3422f101f7e4.png&quot;,
                &quot;Content-Disposition&quot;:&quot;inline; filename=\&quot;Screen Shot 2021-07-15 at 4.00.02 PM.png\&quot;; filename*=UTF-8''Screen%20Shot%202021-07-15%20at%204.00.02%20PM.png&quot;,
                &quot;Content-Type&quot;:&quot;image/png&quot;,
                &quot;policy&quot;:&quot;eyJleHBpcmF0aW9uIjoiMjAyMS0wOC0yN1QyMzoyMDo0M1oiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJmcmFjdGlvbmNsdWItZGV2MiJ9LHsia2V5IjoiY2FjaGUvNDg3MGYxZTFkOWYwNzVjNzM5N2IzNDIyZjEwMWY3ZTQucG5nIn0seyJDb250ZW50LURpc3Bvc2l0aW9uIjoiaW5saW5lOyBmaWxlbmFtZT1cIlNjcmVlbiBTaG90IDIwMjEtMDctMTUgYXQgNC4wMC4wMiBQTS5wbmdcIjsgZmlsZW5hbWUqPVVURi04JydTY3JlZW4lMjBTaG90JTIwMjAyMS0wNy0xNSUyMGF0JTIwNC4wMC4wMiUyMFBNLnBuZyJ9LHsiQ29udGVudC1UeXBlIjoiaW1hZ2UvcG5nIn0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsMCwxMDQ4NTc2MF0seyJ4LWFtei1jcmVkZW50aWFsIjoiQUtJQTNKRVpaRkFOTzNCNlI0VE8vMjAyMTA4MjcvdXMtd2VzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LHsieC1hbXotYWxnb3JpdGhtIjoiQVdTNC1ITUFDLVNIQTI1NiJ9LHsieC1hbXotZGF0ZSI6IjIwMjEwODI3VDIyMjA0M1oifV19&quot;,
                &quot;x-amz-credential&quot;:&quot;whatever/20210827/region/s3/aws4_request&quot;,
                &quot;x-amz-algorithm&quot;:&quot;AWS4-HMAC-SHA256&quot;,
                &quot;x-amz-date&quot;:&quot;20210827T222043Z&quot;,
                &quot;x-amz-signature&quot;:&quot;8478e24c985b7517399dd3278591e283584403c666983b1d6900c3d4c0b594d5&quot;
            },
            &quot;type&quot;:&quot;image/png&quot;,
            &quot;data&quot;:{},
            &quot;progress&quot;:{
                &quot;uploadStarted&quot;:1630102841653,
                &quot;uploadComplete&quot;:true,
                &quot;percentage&quot;:100,
                &quot;bytesUploaded&quot;:167201,
                &quot;bytesTotal&quot;:167201,
                &quot;postprocess&quot;:null
            },
            &quot;size&quot;:165186,
            &quot;isRemote&quot;:false,
            &quot;remote&quot;:&quot;&quot;,
            &quot;preview&quot;:&quot;blob:https://example.com/ab73e53f-7b2c-4a77-aade-07f216b31d27&quot;,
            &quot;xhrUpload&quot;:{
                &quot;method&quot;:&quot;post&quot;,
                &quot;formData&quot;:true,
                &quot;endpoint&quot;:&quot;https://example.s3.region.amazonaws.com&quot;,
                &quot;metaFields&quot;:[
                    &quot;key&quot;,&quot;Content-Disposition&quot;,&quot;Content-Type&quot;,&quot;policy&quot;,&quot;x-amz-credential&quot;,&quot;x-amz-algorithm&quot;,&quot;x-amz-date&quot;,&quot;x-amz-signature&quot;
                ],
                &quot;headers&quot;:{}
            },
            &quot;response&quot;:{
                &quot;status&quot;:204,
                &quot;body&quot;:{
                    &quot;location&quot;:null
                },
                &quot;uploadURL&quot;:null
            },
            &quot;uploadURL&quot;:null,&quot;isPaused&quot;:false
          }
        ],
        &quot;failed&quot;:[],
        &quot;uploadID&quot;:&quot;cksux56ic00013h63szerkigt&quot;
    },

    {
        &quot;successful&quot;:[
            {
                &quot;source&quot;:&quot;Dashboard&quot;,
                &quot;id&quot;:&quot;uppy-blackdog/parkerbright/servitude2017/jpg-2v-2v-1e-image/jpeg-24872-1613012190277&quot;,
                &quot;name&quot;:&quot;blackdog_parkerbright_servitude2017.jpg&quot;,
                &quot;extension&quot;:&quot;jpg&quot;,
                &quot;meta&quot;:{
                    &quot;relativePath&quot;:null,
                    &quot;name&quot;:&quot;blackdog_parkerbright_servitude2017.jpg&quot;,
                    &quot;type&quot;:&quot;image/jpeg&quot;,
                    &quot;key&quot;:&quot;cache/da42d10f2b1f47b004654b257df07a01.jpg&quot;,
                    &quot;Content-Disposition&quot;:&quot;inline; filename=\&quot;blackdog_parkerbright_servitude2017.jpg\&quot;; filename*=UTF-8''blackdog_parkerbright_servitude2017.jpg&quot;,
                    &quot;Content-Type&quot;:&quot;image/jpeg&quot;,
                    &quot;policy&quot;:&quot;eyJleHBpcmF0aW9uIjoiMjAyMS0wOC0yN1QyMzoyMTozMVoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJmcmFjdGlvbmNsdWItZGV2MiJ9LHsia2V5IjoiY2FjaGUvZGE0MmQxMGYyYjFmNDdiMDA0NjU0YjI1N2RmMDdhMDEuanBnIn0seyJDb250ZW50LURpc3Bvc2l0aW9uIjoiaW5saW5lOyBmaWxlbmFtZT1cImJsYWNrZG9nX3BhcmtlcmJyaWdodF9zZXJ2aXR1ZGUyMDE3LmpwZ1wiOyBmaWxlbmFtZSo9VVRGLTgnJ2JsYWNrZG9nX3BhcmtlcmJyaWdodF9zZXJ2aXR1ZGUyMDE3LmpwZyJ9LHsiQ29udGVudC1UeXBlIjoiaW1hZ2UvanBlZyJ9LFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjBdLHsieC1hbXotY3JlZGVudGlhbCI6IkFLSUEzSkVaWkZBTk8zQjZSNFRPLzIwMjEwODI3L3VzLXdlc3QtMS9zMy9hd3M0X3JlcXVlc3QifSx7IngtYW16LWFsZ29yaXRobSI6IkFXUzQtSE1BQy1TSEEyNTYifSx7IngtYW16LWRhdGUiOiIyMDIxMDgyN1QyMjIxMzFaIn1dfQ==&quot;,
                    &quot;x-amz-credential&quot;:&quot;whatever/20210827/region/s3/aws4_request&quot;,
                    &quot;x-amz-algorithm&quot;:&quot;AWS4-HMAC-SHA256&quot;,
                    &quot;x-amz-date&quot;:&quot;20210827T222131Z&quot;,
                    &quot;x-amz-signature&quot;:&quot;a17970d6b111ed0acb535342433626ec7d277fd6364517bb3ee9c02de529f0d7&quot;
                },
                &quot;type&quot;:&quot;image/jpeg&quot;,
                &quot;data&quot;:{},
                &quot;progress&quot;:{
                    &quot;uploadStarted&quot;:1630102891533,
                    &quot;uploadComplete&quot;:true,
                    &quot;percentage&quot;:100,
                    &quot;bytesUploaded&quot;:26864,
                    &quot;bytesTotal&quot;:26864,
                    &quot;postprocess&quot;:null
                },
                &quot;size&quot;:24872,
                &quot;isRemote&quot;:false,
                &quot;remote&quot;:&quot;&quot;,
                &quot;preview&quot;:&quot;blob:https://example.com/79066114-d51b-4ef4-b62a-af8ac80ae617&quot;,
                &quot;xhrUpload&quot;:{
                    &quot;method&quot;:&quot;post&quot;,
                    &quot;formData&quot;:true,
                    &quot;endpoint&quot;:&quot;https://example.s3.region.amazonaws.com&quot;,
                    &quot;metaFields&quot;:[
                        &quot;key&quot;,&quot;Content-Disposition&quot;,&quot;Content-Type&quot;,&quot;policy&quot;,&quot;x-amz-credential&quot;,&quot;x-amz-algorithm&quot;,&quot;x-amz-date&quot;,&quot;x-amz-signature&quot;
                    ],
                    &quot;headers&quot;:{}
                },
                &quot;response&quot;:{
                    &quot;status&quot;:204,
                    &quot;body&quot;:{
                        &quot;location&quot;:null
                    },
                    &quot;uploadURL&quot;:null
                },
                &quot;uploadURL&quot;:null,
                &quot;isPaused&quot;:false
            }
        ],
        &quot;failed&quot;:[],
        &quot;uploadID&quot;:&quot;cksux68zv00033h63tzjc85kg&quot;
    }
  ]"
>

When I log document.getElementsByName("uppyResult") to console, it shows up as a NodeList. Which apparently is sort of like an array, but not? The data I need to access in this NodeList takes a click and a scroll to get to - I simply expand the input and scroll to defaultValue.

What I need is access to every successful object in this NodeList. There can be one or many. Note that this one has two.

My last attempt at doing this same thing was botched because I misunderstood what I was trying to do. Though the selected answer did solve the problem as I phrased it, what I needed was to perform the same action for more than just the initial [0] item. Each uploaded batch of files occupies an index in this array, and I need to get all of them.

The old code from that answer is:

if (reason === 'removed-by-user') {
  const preParsedData = document.getElementsByName("uppyResult")[0].value //this is the problem, i think - i need *all* the elements in the array/NodeList
  const parsedData = JSON.parse(preParsedData)
  const toRemove = file.id
  parsedData.forEach(item => {
    Object.values(item).forEach(array => {
      if (!Array.isArray(array))
        return;
      const index = array.findIndex(elm => elm.id === toRemove)
      if (index > -1)
        array.splice(index, 1)
    });
    if (item.successful.length === 0)
      delete item.successful
      delete item.failed
      delete item.uploadID
    });
    console.log(parsedData)
    document.getElementsByName("uppyResult")[0].value = JSON.stringify(parsedData)
  }
})
calyxofheld
  • 1,538
  • 3
  • 24
  • 62
  • 1
    Your first code block is not valid HTML. Problem with quotes. Even the syntax highlighting here shows that it's wrong. – trincot Aug 27 '21 at 23:18
  • @trincot should be fixed now. i changed all the `"` to `"` so i could read it while linting, and then forgot to switch it back – calyxofheld Aug 27 '21 at 23:30

2 Answers2

2

In case if you "need all the elements in the array/NodeList" you have to go through all nodes with other forEach call. So, it will looks like:

const inputEls = document.getElementsByName("uppyResult")

inputEls.forEach((node) => {
// here run the code you already have
const preParsedData = node.value
const parsedData = JSON.parse(preParsedData)
....
}
ShevchenkoVV
  • 131
  • 5
  • hmmm this does not work. shouldn't i be able to log the results of `node` to console? – calyxofheld Aug 28 '21 at 00:09
  • 1
    You should, except there is no items in inputEls. The *node* should be an element that has name equals to *uppyResult*. Do you still have inputs with 'uppyResult' names? – ShevchenkoVV Aug 28 '21 at 00:16
  • hm, i'm sort of confused by what you mean by "inputs with `uppyResult` names". what i have is a json string that gets inserted into an input named `uppyResult` – calyxofheld Aug 28 '21 at 04:15
  • 1
    I'll try to explain: `document.getElementsByName({any_name})` - returns a collection of NodeList that will be all elements with name equals to`{any_name}` from the document. Result can be collection of 1 item, 2 items, etc. To go though all of them and do not hardcode the collection index`[0]` (like you mentioned as a problem), you have to execute the forEach method of NodeList collection with function that you need to apply to each element of NodeList collection. In case if you have several inputs with different names, I'd recommend to look at ```getElementsByClassName``` – ShevchenkoVV Aug 28 '21 at 10:11
  • im still a little bit lost. `const inputEls = document.getElementsByName("uppyResult")` should return the input value, right? calling `.length` on that is `undefined` – calyxofheld Sep 01 '21 at 01:22
  • no, that's a NodeList again, as you observed in your original question. That's why you need to iterate across it rather than access properties directly - and this answer is showing you how to do it. – Robin Zigmond Sep 07 '21 at 16:45
1

Before you parse the data in value it has to be a valid JSON string. For that, you have to replace every instance of &quot; by " using the .replace() method so that it becomes a valid JSON string to be parsed.

const preParsedData = document.getElementsByName("uppyResult")[0].value.replace("&quot;","\"");
const parsedData = JSON.parse(preParsedData);
console.log(parsedData);
<html>
<head>
  <title>Title</title>
</head>
<body>
  <input name="uppyResult" type="hidden" value="[
    {
        &quot;successful&quot;:[
            {
            &quot;source&quot;:&quot;Dashboard&quot;,
            &quot;id&quot;:&quot;uppy-screen/shot/2021/07/15/at/4/00/02/pm/png-10-10-1d-1d-10-10-1e-1e-10-1e-image/png-165186-1626390007842&quot;,
            &quot;name&quot;:&quot;Screen Shot 2021-07-15 at 4.00.02 PM.png&quot;,
            &quot;extension&quot;:&quot;png&quot;,
            &quot;meta&quot;:{
                &quot;relativePath&quot;:null,
                &quot;name&quot;:&quot;Screen Shot 2021-07-15 at 4.00.02 PM.png&quot;,
                &quot;type&quot;:&quot;image/png&quot;,
                &quot;key&quot;:&quot;cache/4870f1e1d9f075c7397b3422f101f7e4.png&quot;,
                &quot;Content-Disposition&quot;:&quot;inline; filename=\&quot;Screen Shot 2021-07-15 at 4.00.02 PM.png\&quot;; filename*=UTF-8''Screen%20Shot%202021-07-15%20at%204.00.02%20PM.png&quot;,
                &quot;Content-Type&quot;:&quot;image/png&quot;,
                &quot;policy&quot;:&quot;eyJleHBpcmF0aW9uIjoiMjAyMS0wOC0yN1QyMzoyMDo0M1oiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJmcmFjdGlvbmNsdWItZGV2MiJ9LHsia2V5IjoiY2FjaGUvNDg3MGYxZTFkOWYwNzVjNzM5N2IzNDIyZjEwMWY3ZTQucG5nIn0seyJDb250ZW50LURpc3Bvc2l0aW9uIjoiaW5saW5lOyBmaWxlbmFtZT1cIlNjcmVlbiBTaG90IDIwMjEtMDctMTUgYXQgNC4wMC4wMiBQTS5wbmdcIjsgZmlsZW5hbWUqPVVURi04JydTY3JlZW4lMjBTaG90JTIwMjAyMS0wNy0xNSUyMGF0JTIwNC4wMC4wMiUyMFBNLnBuZyJ9LHsiQ29udGVudC1UeXBlIjoiaW1hZ2UvcG5nIn0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsMCwxMDQ4NTc2MF0seyJ4LWFtei1jcmVkZW50aWFsIjoiQUtJQTNKRVpaRkFOTzNCNlI0VE8vMjAyMTA4MjcvdXMtd2VzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LHsieC1hbXotYWxnb3JpdGhtIjoiQVdTNC1ITUFDLVNIQTI1NiJ9LHsieC1hbXotZGF0ZSI6IjIwMjEwODI3VDIyMjA0M1oifV19&quot;,
                &quot;x-amz-credential&quot;:&quot;whatever/20210827/region/s3/aws4_request&quot;,
                &quot;x-amz-algorithm&quot;:&quot;AWS4-HMAC-SHA256&quot;,
                &quot;x-amz-date&quot;:&quot;20210827T222043Z&quot;,
                &quot;x-amz-signature&quot;:&quot;8478e24c985b7517399dd3278591e283584403c666983b1d6900c3d4c0b594d5&quot;
            },
            &quot;type&quot;:&quot;image/png&quot;,
            &quot;data&quot;:{},
            &quot;progress&quot;:{
                &quot;uploadStarted&quot;:1630102841653,
                &quot;uploadComplete&quot;:true,
                &quot;percentage&quot;:100,
                &quot;bytesUploaded&quot;:167201,
                &quot;bytesTotal&quot;:167201,
                &quot;postprocess&quot;:null
            },
            &quot;size&quot;:165186,
            &quot;isRemote&quot;:false,
            &quot;remote&quot;:&quot;&quot;,
            &quot;preview&quot;:&quot;blob:https://example.com/ab73e53f-7b2c-4a77-aade-07f216b31d27&quot;,
            &quot;xhrUpload&quot;:{
                &quot;method&quot;:&quot;post&quot;,
                &quot;formData&quot;:true,
                &quot;endpoint&quot;:&quot;https://example.s3.region.amazonaws.com&quot;,
                &quot;metaFields&quot;:[
                    &quot;key&quot;,&quot;Content-Disposition&quot;,&quot;Content-Type&quot;,&quot;policy&quot;,&quot;x-amz-credential&quot;,&quot;x-amz-algorithm&quot;,&quot;x-amz-date&quot;,&quot;x-amz-signature&quot;
                ],
                &quot;headers&quot;:{}
            },
            &quot;response&quot;:{
                &quot;status&quot;:204,
                &quot;body&quot;:{
                    &quot;location&quot;:null
                },
                &quot;uploadURL&quot;:null
            },
            &quot;uploadURL&quot;:null,&quot;isPaused&quot;:false
          }
        ],
        &quot;failed&quot;:[],
        &quot;uploadID&quot;:&quot;cksux56ic00013h63szerkigt&quot;
    },

    {
        &quot;successful&quot;:[
            {
                &quot;source&quot;:&quot;Dashboard&quot;,
                &quot;id&quot;:&quot;uppy-blackdog/parkerbright/servitude2017/jpg-2v-2v-1e-image/jpeg-24872-1613012190277&quot;,
                &quot;name&quot;:&quot;blackdog_parkerbright_servitude2017.jpg&quot;,
                &quot;extension&quot;:&quot;jpg&quot;,
                &quot;meta&quot;:{
                    &quot;relativePath&quot;:null,
                    &quot;name&quot;:&quot;blackdog_parkerbright_servitude2017.jpg&quot;,
                    &quot;type&quot;:&quot;image/jpeg&quot;,
                    &quot;key&quot;:&quot;cache/da42d10f2b1f47b004654b257df07a01.jpg&quot;,
                    &quot;Content-Disposition&quot;:&quot;inline; filename=\&quot;blackdog_parkerbright_servitude2017.jpg\&quot;; filename*=UTF-8''blackdog_parkerbright_servitude2017.jpg&quot;,
                    &quot;Content-Type&quot;:&quot;image/jpeg&quot;,
                    &quot;policy&quot;:&quot;eyJleHBpcmF0aW9uIjoiMjAyMS0wOC0yN1QyMzoyMTozMVoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJmcmFjdGlvbmNsdWItZGV2MiJ9LHsia2V5IjoiY2FjaGUvZGE0MmQxMGYyYjFmNDdiMDA0NjU0YjI1N2RmMDdhMDEuanBnIn0seyJDb250ZW50LURpc3Bvc2l0aW9uIjoiaW5saW5lOyBmaWxlbmFtZT1cImJsYWNrZG9nX3BhcmtlcmJyaWdodF9zZXJ2aXR1ZGUyMDE3LmpwZ1wiOyBmaWxlbmFtZSo9VVRGLTgnJ2JsYWNrZG9nX3BhcmtlcmJyaWdodF9zZXJ2aXR1ZGUyMDE3LmpwZyJ9LHsiQ29udGVudC1UeXBlIjoiaW1hZ2UvanBlZyJ9LFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjBdLHsieC1hbXotY3JlZGVudGlhbCI6IkFLSUEzSkVaWkZBTk8zQjZSNFRPLzIwMjEwODI3L3VzLXdlc3QtMS9zMy9hd3M0X3JlcXVlc3QifSx7IngtYW16LWFsZ29yaXRobSI6IkFXUzQtSE1BQy1TSEEyNTYifSx7IngtYW16LWRhdGUiOiIyMDIxMDgyN1QyMjIxMzFaIn1dfQ==&quot;,
                    &quot;x-amz-credential&quot;:&quot;whatever/20210827/region/s3/aws4_request&quot;,
                    &quot;x-amz-algorithm&quot;:&quot;AWS4-HMAC-SHA256&quot;,
                    &quot;x-amz-date&quot;:&quot;20210827T222131Z&quot;,
                    &quot;x-amz-signature&quot;:&quot;a17970d6b111ed0acb535342433626ec7d277fd6364517bb3ee9c02de529f0d7&quot;
                },
                &quot;type&quot;:&quot;image/jpeg&quot;,
                &quot;data&quot;:{},
                &quot;progress&quot;:{
                    &quot;uploadStarted&quot;:1630102891533,
                    &quot;uploadComplete&quot;:true,
                    &quot;percentage&quot;:100,
                    &quot;bytesUploaded&quot;:26864,
                    &quot;bytesTotal&quot;:26864,
                    &quot;postprocess&quot;:null
                },
                &quot;size&quot;:24872,
                &quot;isRemote&quot;:false,
                &quot;remote&quot;:&quot;&quot;,
                &quot;preview&quot;:&quot;blob:https://example.com/79066114-d51b-4ef4-b62a-af8ac80ae617&quot;,
                &quot;xhrUpload&quot;:{
                    &quot;method&quot;:&quot;post&quot;,
                    &quot;formData&quot;:true,
                    &quot;endpoint&quot;:&quot;https://example.s3.region.amazonaws.com&quot;,
                    &quot;metaFields&quot;:[
                        &quot;key&quot;,&quot;Content-Disposition&quot;,&quot;Content-Type&quot;,&quot;policy&quot;,&quot;x-amz-credential&quot;,&quot;x-amz-algorithm&quot;,&quot;x-amz-date&quot;,&quot;x-amz-signature&quot;
                    ],
                    &quot;headers&quot;:{}
                },
                &quot;response&quot;:{
                    &quot;status&quot;:204,
                    &quot;body&quot;:{
                        &quot;location&quot;:null
                    },
                    &quot;uploadURL&quot;:null
                },
                &quot;uploadURL&quot;:null,
                &quot;isPaused&quot;:false
            }
        ],
        &quot;failed&quot;:[],
        &quot;uploadID&quot;:&quot;cksux68zv00033h63tzjc85kg&quot;
    }
  ]">
</body>
</html>