Node.js 如何从nodejs中的json对象获取值
我无法从nodejs中的json对象获取值。我总是收到json对象键的未定义消息。这可能是nodejs中的响应sync或asyn。我不知道如何解决此问题。任何人都可以解决此问题吗 data.controller.jsNode.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
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);