node.js - 如何从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
}
解决方案
使用 JSON.parse()。
let collectionDataJSON = JSON.parse(collectionData);
console.log(collectionDataJSON.product_name);
推荐阅读
- mongodb - MongoDB v4 explain() 不返回扫描文档的数量(与以前的版本不同)。怎样成为?
- python - 将 keras (.h5) 模型转换为 tf 冻结图 (.pd) 格式后的精度损失
- java - 用户存在返回值不可读
- grails - 如何知道grails中上传的文件大小?
- node.js - TypeError:res.setHeader 不是函数,在启用 cors 时出现此错误
- python-3.x - 如何使用 python 返回所有嵌套对象?
- react-native - 反应原生 Android 构建错误 - react-native-square-in-app-payments
- java - 在java中创建自定义收集器
- c# - 为什么我不能在继承的类中使用继承的对象?
- gstreamer - [gstreamer]:GstAggregator 的问题