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

试图从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

}


慕虎7371278
浏览 155回答 1
1回答

开满天机

您必须解析字符串才能使用 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);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript