Jelajahi Sumber

Try/catch JSON.parse, since the prev result can be empty or not json; set updatedResult to an empty array if it’s not an array (#1800)

Artur Paikin 5 tahun lalu
induk
melakukan
45567c0b13
1 mengubah file dengan 13 tambahan dan 2 penghapusan
  1. 13 2
      packages/@uppy/form/src/index.js

+ 13 - 2
packages/@uppy/form/src/index.js

@@ -88,8 +88,19 @@ module.exports = class Form extends Plugin {
     let resultInput = this.form.querySelector(`[name="${this.opts.resultName}"]`)
     if (resultInput) {
       if (this.opts.multipleResults) {
-        // Append new result to the previous result array
-        const updatedResult = JSON.parse(resultInput.value)
+        // Append new result to the previous result array.
+        // If the previous result is empty, or not an array,
+        // set it to an empty array.
+        let updatedResult
+        try {
+          updatedResult = JSON.parse(resultInput.value)
+        } catch (err) {
+          // Nothing, since we check for array below anyway
+        }
+
+        if (!Array.isArray(updatedResult)) {
+          updatedResult = []
+        }
         updatedResult.push(result)
         resultInput.value = JSON.stringify(updatedResult)
       } else {