简体   繁体   English

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

[英]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.

相关问题 从数组获取对象值-NodeJs - Get object values from array - NodeJs 我如何获取 get api 调用的数据,格式为 reactjs 中 object 数组中的 object 嵌套数组? - How can i fetch the data of an get api call which is in the format as nested array of object in array of object in reactjs? 如何正确导出从NodeJS中的API调用检索到的值 - How to properly export values retrieved from an API call in NodeJS 如何解决“NodeJS API 调用响应是不可解析的对象”? - how to resolve 'NodeJS API call response is an un-parsable object'? NodeJS 求和数组中的 object 个值 - NodeJS sum the object values in the array How to only get first object of an array from API call and transform it and pass the object to the assembleMyModel function in RxJS? - How to only get first object of an array from API call and transform it and pass the object to the assembleMyModel function in RxJS? 如何在NodeJS中调用登录API并在命令提示符下获取输出? - How to call login API and get output on command prompt in nodeJS? 如何获取对象数组值到对象中的其他数组值? - How to get object array values to other array values within the object? 如何捕获通过HTTP GET的api调用返回的Array对象? - How can I capture the Array object returned by an api call through HTTP GET? 在NodeJS API调用中将数组中的元素推入和拉出 - Push and Pull elements in Array in NodeJS API call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM