[英]How to get array object values by nodejs api call
Trying to get value from nodejs api call but i am getting like [object Object] So How to get values from an array object?试图从 nodejs api 调用中获取值,但我越来越喜欢 [object Object] 那么如何从数组 object 中获取值?
product.component.ts:产品.component.ts:
this.sendDatatoInsertDb(this.selectedProduct.replace(/\s/g,''),JSON.stringify(this.registerForm.value));
sendDatatoInsertDb(collection,collectionData) {
this.adminService.insertDataintoDb(collection,collectionData).subscribe(
res => { },
err => { console.log(err); }
);
}
this.registerForm.value is like this.registerForm.value 就像
{
"p_id": "C5",
"product_name": "name",
"product_weight": "250gm"
}
admin.service.ts: admin.service.ts:
insertDataintoDb(collection,insertData){
return this.http.get(environment.apiBaseUrl + '/insertData?collection='+collection+'&collectionData='+insertData);
}
product.controller.js:产品.controller.js:
module.exports.insertData = (req, res, next) => {
let collectionName = req.query.collection;
let collectionData = req.query.collectionData;
console.log(collectionData); //Getting [object Object]
console.log(collectionData.p_id); //Getting undefined
console.log(collectionData.product_name); //Getting undefined
console.log(collectionData.product_weight); //Getting undefined
}
You have to parse the string to get your object back using JSON.parse.您必须使用 JSON.parse 解析字符串以取回您的 object。
module.exports.insertData = (req, res, next) => {
let collectionName = req.query.collection;
let collectionData = req.query.collectionData;
let collectionDataJSON = JSON.parse(collectionData);
console.log(collectionDataJSON.p_id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.