How to append an object?
See original GitHub issueHello need to append object field something like:
var form = new FormData();
form.append('name', 'Name');
form.append('details', {age: 12});
but it will throw an error like this:
TypeError: Object #<Object> has no method 'on'
at Function.DelayedStream.create (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at FormData.CombinedStream.append (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/combined-stream/lib/combined_stream.js:43:37)
at FormData.append (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/form-data/lib/form_data.js:43:3)
at validationFailTest (/home/pity/workspace/node_modules/cmp-uploaders/test/lab/simple_uploader.js:68:6)
at Object._onImmediate (/home/pity/workspace/node_modules/cmp-uploaders/node_modules/lab/lib/execute.js:492:17)
at processImmediate [as _immediateCallback] (timers.js:330:15)
Issue Analytics
- State:
- Created 9 years ago
- Comments:16 (3 by maintainers)
Top Results From Across the Web
Appending to an object - javascript
You can do this with Object.assign(). Sometimes you need an array, but when working with functions that expect a single JSON object --...
Read more >How to Append Values to Object in JavaScript
The Object.assign() method appending values to objects by key/value pairs. The push() method adds one or multiple values into an array. However, the...
Read more >JavaScript append new elements to an object
When you need to append new elements to your JavaScript object variable, you can use either the Object.assign() method or the spread operator....
Read more >Append Values to Object in JavaScript
Use the object.assign() Method to Append Elements to Objects in JavaScript ... The object.assign() method will copy all properties defined in an ...
Read more >JavaScript Program to Append an Object to An Array
In the above program, the push() method is used to add an object to an array. The push() method adds an item to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey,
The thing is you can send only strings over the wire, so complex data types, like object should be encoded somehow and since there are many way to encode objects, FormData leaves it to developer to choose proper encoding mechanism that supported on the other end. For example it could be a
JSON
string or aform-urlencoded
string.Something like:
Let me know if you have more questions.
I was trying to post a complex data structure without pickling it into json to set up a simple data flow from React to Rails, and this thread was the most helpful piece of information. Just in case someone is looking for an example:
data to be posted:
implementation with
FormData
: