0

I am not able to get the values from json object in nodejs.I am getting always undefined message for json object key.May be this response sync or asyn in nodejs.I do not know how to resolve this issue.Anyone can resolve this issue?

data.controller.js

module.exports.insertData = (req, res, next) => { 
let collectionName = req.query.collection; 
let collectionData = req.query.collectionData;
 console.log(collectionData);//Getting {"product_name":"test","product_weight":"45","product_price":"362"}
 console.log(collectionData.product_name); //Getting undefined
 console.log(collectionData.product_price); //Getting undefined
 console.log(collectionData.product_weight); //Getting undefined
 }
asked May 16, 2020 at 14:55

2 Answers 2

1

Use JSON.parse().

let collectionDataJSON = JSON.parse(collectionData);
console.log(collectionDataJSON.product_name);
answered May 16, 2020 at 15:08
Sign up to request clarification or add additional context in comments.

Comments

0

You can use lodash library for your util methods,

npm i lodash

After installation:

import _ from 'lodash';
const productName = _.get(collectionData, 'product_name', 'default_name')
console.log(productName);
answered May 16, 2020 at 15:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.