首页 > 解决方案 > 如何从nodejs中的json对象获取值

问题描述

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

数据控制器.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
 }

标签: node.js

解决方案


使用 JSON.parse()。

let collectionDataJSON = JSON.parse(collectionData);

console.log(collectionDataJSON.product_name);

推荐阅读