Trying to submit a form using formgroup, Initially the for value was an object but after appending to formdata the value becomes string so that i cannot parse the data in query.
users form value after submit:-
[
{
"id": "",
"employee": "6166d234379ec5e4c9b3fe3",
"position": "Manager",
"users": [
"715ea5df332403e4215324234",
"815ea33534534333e42153432"
],
}
]
Object.keys(this.Form.controls).forEach(key => {
if(key == 'users'){
console.log(typeof(this.Form.get(key).value)) //type is object
formData.append(key,this.Form.get(key).value);
}
});
formData.forEach((value,key) => {
console.log(key+" "+typeof(value)) // type of value becomes string
});
//trying covert it to object
formData.forEach((value,key) => {
if(key == 'users'){
formData.set(key,JSON.parse(JSON.stringify(value)));
//formData.set(key,JSON.parse(value)); //return error SyntaxError: Unexpected token o
in JSON at position
console.log(key+" "+typeof(value)) // still string
}
});
need to submit the formdata with users as object
FormData can only be simple key/value pairs of strings. If you must use a FormData object, you can experiment with appending your values as json-encoded strings:
formData.append(key, JSON.stringify(this.Form.get(key).value));
This will allow you to parse the values later:
formData.forEach((value, key) => {
console.log(key, JSON.parse(value));
});