Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何从nodejs中的json对象获取值_Node.js - Fatal编程技术网

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

Node.js 如何从nodejs中的json对象获取值,node.js,Node.js,我无法从nodejs中的json对象获取值。我总是收到json对象键的未定义消息。这可能是nodejs中的响应sync或asyn。我不知道如何解决此问题。任何人都可以解决此问题吗 data.controller.js module.exports.insertData = (req, res, next) => { let collectionName = req.query.collection; let collectionData = req.query.collectionDa

我无法从nodejs中的json对象获取值。我总是收到json对象键的未定义消息。这可能是nodejs中的响应sync或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);
log(collectionDataJSON.product_name);
您可以将库用于util方法

npm i lodash
安装后:

import _ from 'lodash';

const productName = _.get(collectionData, 'product_name', 'default_name')

console.log(productName);