Stack Exchange
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?
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 is like 
{
        "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.stringfy(object) try this

@SaadSohail : Getting error : TypeError: JSON.stringfy is not a function

ur question is not clear. please add proper details

let response = JSON.stringfy(collectionData) object is the response that you are getting from api

You have to parse the string to get your object back using 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);

}


Getting error :TypeError: collectionData.each is not a function

I update the code to forEach. Forgot that each is not a javascript method. My bad lol @KavithaK

Getting error: TypeError: collectionData.forEach is not a function

I am suspecting that your array is return as a string. You might have to parse it first. Replace let collectionData = req.query.collectionData; with let collectionData = JSON.parse(req.query.collectionData);. To really help you i will need to know the type of collectionData but i think this will help

Getting error: SyntaxError: Unexpected token o in JSON at position 1

Please can you get the type of collectionData. Try to expand the result of console.log on the console and paste what you get. Its difficult working without seeing what the result looks like. Thanks

console.log(req.query.collectionData)  is  {
"p_id": "C5",
"product_name": "name",
"product_weight": "250gm"
}

I see your problem now. Check my updated answer above.

The updated answer above does not have item. Please check again

Getting undefined

console.log(collectionData["p_id"]);
console.log(collectionData["product_name"]);
console.log(collectionData["product_weight"]); These are getting undefined message

I will go out on a limp here. I think it is because they are run before there is a response from the server. This means that you will always get undefined. However you can wait for the server to response before you run those lines. Wrap up req.query.collectionData in a promise and wait for the response before running those lines. If you need help with this let me know

Yes.I need your help

To be sure that its the query issue include this code in the snippet setTimeout(function() { console.log(collectionData["p_id"]); console.log(collectionData["product_name"]); console.log(collectionData["product_weight"]) }, 4000); and then run. Tell me the results

Again i am getting same undefined

You got any solution?

sorry for the late reply. I have been looking at the code and just can't wrap my head around what is going on. Please can you try to expand the value of console.log(collectionData) in the console and lets see what it returns

console.log(collectionData) returns { "p_id": "C5", "product_name": "name", "product_weight": "250gm" }

what does this give you, this is kinda hard to follow up. JSON.parse(JSON.stringify(a)).p_id

This line is working :let collectionDataJSON = JSON.parse(collectionData);

Okay we tried that already right? give me feedback so that i can update the answer to help others viewing the question
