I have an object postData which comes to the controller in form of query when I console log the object it has data:-
console.log("data:- ", req.query.postData);
console output:-
data:- {
"property1":"value1",
"property2":"value2",
"property3":"value3",
"property4":"value4"
}
But when trying to restructure individual property:-
const {property1,property2,property3,property4} = req.query.postData;
console.log(property1);
console.log(property2);
console.log(property3);
console.log(property4);
console output:-
It is undefined in console log
>Solution :
postData is just a string. You need to convert it to an object with JSON.parse()
const {property1,property2,property3,property4} = JSON.parse(req.query.postData);
I don’t think passing JSON in the query string is really a good idea, though.
** instead, you could send it in the request body.
Or, for the query string use
?property1=value1&property2=value2&property3=value3&property4=value4