Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Express Query object undefined

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:-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading