0

Hello Stackoverflower,

a quick question:

I would like to create a DataForm in JavaScript, push the DataForm into an array, send the array to another function, modify in the new function the DataForm and then post the DataForm.

I implemented this like the following abstract, where function1() populates the array with many FormData(), and function2() retrieves each FormData within the array and .set() a new value to the key:

var arrayTempIds = [];

function function1() {
  var formData = new FormData();
  formData.append("id", value);
  arrayTempIds.push(formData);
};

function function2() {
  for(var i = 0; i < arrayTempIds.length; i++) {
    newId = arrayTempIds[i];
    newId.set("id", returndata.id);
  }
};

Everything works except the fact that it seems that I cannot write arrayTempIds[i].set(), as the error that comes is:

Uncaught TypeError: undefined is not a function

Does anyone know why?

Luca

TakeMeToTheMoon
  • 527
  • 1
  • 8
  • 25
  • on wich line are you getting the error? What is returndata? – Moppo Jun 15 '15 at 13:22
  • Is this the full code? Can you create a http://jsfiddle.net/ – meteor Jun 15 '15 at 13:23
  • @Moppo, Just after the newId.set(...). In other words, if I remove that line, all works fine, using the "id" defined in function1(). Returndata is the ajax response to the POST method. In other words, I say: if the response is a success, provide the new Id (returndata.id) and substitute this id to the previous one. – TakeMeToTheMoon Jun 15 '15 at 13:29
  • i think the problem is you are calling the method `set` on a variable (`newId`) that doesn't have that method – Moppo Jun 15 '15 at 13:31
  • @Moppo I am using this: https://developer.mozilla.org/en/docs/Web/API/FormData – TakeMeToTheMoon Jun 15 '15 at 13:37
  • An example is here: http://www.javascripture.com/FormData – TakeMeToTheMoon Jun 15 '15 at 13:40
  • try a `console.debug(newId)` before the line where you do `newId.set` – Moppo Jun 15 '15 at 13:53
  • @Moppo The answer is -> FormData – TakeMeToTheMoon Jun 15 '15 at 14:00
  • @Moppo, No sorry... now it is: FormData {append: function} __proto__: FormData – TakeMeToTheMoon Jun 15 '15 at 14:02
  • try to set a mock value like `newId.set("id", "test");` to see if it works, if all the array's elements are of type FormData it should work – Moppo Jun 15 '15 at 14:09
  • @Moppo This is a good observation. It doesn't work. I even wrote newId.set() after .append() in function1(){} and it didn't work neither there. – TakeMeToTheMoon Jun 15 '15 at 14:20
  • in function1 you have just created your object so it should work. Maybe FormData isn't working properly ? – Moppo Jun 15 '15 at 14:24
  • @Moppo That's possible. Neither the function .get() works. So probably the webpage I was referring to is not properly updated. Maybe FormData does have only the append() method – TakeMeToTheMoon Jun 15 '15 at 14:40
  • I found this Stackoverflow post that says there is no method other than .append() for FormData : http://stackoverflow.com/questions/21443553/how-to-remove-value-from-formdata – TakeMeToTheMoon Jun 15 '15 at 15:28

1 Answers1

0

I believe set() is not a defined JavaScript function that assigns the value to an array the way you are trying to do.

Try arrayTempIds[i] = someValue;