试图从nodejs api调用中获取值,但我得到了像[对象对象]那么如何从数组对象中获取值?product.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 就像{ "p_id": "C5", "product_name": "name", "product_weight": "250gm"}admin.service.ts: insertDataintoDb(collection,insertData){ return this.http.get(environment.apiBaseUrl + '/insertData?collection='+collection+'&collectionData='+insertData); }产品.控制器.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}
1 回答

开满天机
TA贡献1786条经验 获得超13个赞
您必须解析字符串才能使用 JSON.parse 取回对象。
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);
}
添加回答
举报
0/150
提交
取消