为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过 nodejs api 调用获取数组对象值

如何通过 nodejs api 调用获取数组对象值

慕虎7371278 2022-08-18 10:36:06
试图从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);


}


查看完整回答
反对 回复 2022-08-18
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号