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
CodePudding user response:
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
CodePudding user response:
It might be possible that the data you are getting is a type string not an object, you need to parse the string first then destructure it.
const {property1,property2,property3,property4} = JSON.parse(req.query.postData)
In general, req.query is a string not an object. I suggest you to convert your request from GET
to POST
and obtain these data from req.body
instead of req.query
, it will be much simpler and easy also a much preferred way.