Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过nodejsapi调用获取数组对象值_Javascript_Node.js - Fatal编程技术网

Javascript 如何通过nodejsapi调用获取数组对象值

Javascript 如何通过nodejsapi调用获取数组对象值,javascript,node.js,Javascript,Node.js,试图从nodejsapi调用中获取值,但我越来越像[object object],那么如何从数组对象中获取值呢 product.component.ts: this.sendDatatoInsertDb(this.selectedProduct.replace(/\s/g,''),JSON.stringify(this.registerForm.value)); sendDatatoInsertDb(collection,collectionData) { this.adminS

试图从nodejsapi调用中获取值,但我越来越像[object object],那么如何从数组对象中获取值呢

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); }
    ); 
  }
此.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);
  }
product.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
}

您必须解析字符串,才能使用JSON.parse返回对象

module.exports.insertData=(req,res,next)=>{
让collectionName=req.query.collection;
让collectionData=req.query.collectionData;
让collectionDataJSON=JSON.parse(collectionData);
log(collectionDataJSON.p_id);
}

JSON.stringfy(对象)尝试this@SaadSohail:获取错误:TypeError:JSON.stringfy不是函数我们的问题不清楚。请添加适当的详细信息let response=JSON。stringfy(collectionData)对象是您从APIGENT error:TypeError:collectionData获得的响应。每个都不是函数我将代码更新为forEach。忘记了每个都不是javascript方法。我的坏lol@KavithaKGetting error:TypeError:collectionData.forEach不是一个函数我怀疑数组是以字符串形式返回的。您可能必须首先解析它。替换
let collectionData=req.query.collectionData
with
let collectionData=JSON.parse(req.query.collectionData)。为了真正帮助您,我需要知道collectionData的类型,但我认为这将有助于获取错误:SyntaxError:JSON中位置1处的意外标记o