如何从Node.js中的JSON对象获取值

Kavitha K:

我无法从nodejs中的json对象获取值。我总是得到json对象密钥的未定义消息。可能是nodejs中的此响应同步或asyn。我不知道如何解决此问题。任何人都可以解决此问题?

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
 }
阿尔伯特:

使用JSON.parse()。

let collectionDataJSON = JSON.parse(collectionData);

console.log(collectionDataJSON.product_name);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章