0

I am trying to get some specific field from mongodb json response throught my node.js get request. But it gives me undefiend.

    app.get('/continueDiscovery', addon.authenticate(), function(req, res) {
    console.log("Calling to getAllDiscovery")
    productDiscovery.find(function (err, data){

        if (err)
            res.send(err)

        var data = JSON.stringify(data);

        var jsonData = JSON.parse(data)
        console.log("Getting all data")
        console.log(jsonData.productName)

        console.log(data)

        res.render('continueDiscovery', {continueDiscovery: data });
    });
});

console.log(data) prints

[{"_id":"56989b14814fa7f664000001","__v":0},{"_id":"56989d302e88c32c0a000001","__v":0},{"_id":"56989d6373c4163010000001","__v":0},{"_id":"5698a5a03fb53c451e000001","__v":0},{"_id":"5698a5b23fb53c451e000002","__v":0},{"_id":"5698a65e5978b95b27000001","__v":0},{"_id":"5698a66b5978b95b27000002","__v":0},{"_id":"5698aac4973c3b9864000001","__v":0},{"_id":"5698ac666a9411e70f000001","__v":0},{"_id":"5698ac906a9411e70f000002","__v":0},{"_id":"5698bc34d7b926f518000001","__v":0},{"_id":"5698bc83c123996720000001","__v":0},{"_id":"5698bd57e356ef2b26000001","__v":0},{"_id":"5698bf9ef45490fd54000001","__v":0},{"_id":"5698c009636c05615c000001","__v":0},{"_id":"5698c04d4391848a60000001","__v":0},{"_id":"5698c068d760666863000001","__v":0},{"_id":"5698c0b7ce51651468000001","__v":0},{"_id":"5698c1124b365c3d6f000001","__v":0},{"_id":"5698c1331d0d460872000001","__v":0},{"_id":"5698c14f1d0d460872000002","__v":0},{"_id":"5698c470bfc2076c2a000001","__v":0},{"_id":"5698c487bc25c41a2e000001","__v":0},{"_id":"5698c4e7c71b3d7633000001","__v":0},{"_id":"5698c51887c28f5c36000001","__v":0},{"_id":"5698c5d34981174643000001","__v":0},{"_id":"5698cbf39eda14852c000001","__v":0},{"_id":"5698cc653753891c33000001","__v":0},{"_id":"5698ccd080698b5d3b000001","__v":0},{"_id":"5698cd0926cad98340000001","__v":0},{"_id":"5698d21a0e2c9a6715000001","__v":0},{"_id":"5698d31a0f410ec925000001","__v":0},{"_id":"5698d589f3a2fbb63c000001","__v":0},{"_id":"5698d92bb34a3e4867000001","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92db34a3e4867000002","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92db34a3e4867000003","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92db34a3e4867000004","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92eb34a3e4867000005","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92eb34a3e4867000006","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92eb34a3e4867000007","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92eb34a3e4867000008","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d92eb34a3e4867000009","productName":"ewfewf","endUsers":"ewfewfew","__v":0},{"_id":"5698d955b34a3e486700000a","productName":"alokwa re","endUsers":"ewfewfew","__v":0},{"_id":"5698da92596d27f625000001","productName":"re alokwa","endUsers":"re userwa","problemsArea":"re problemwa","productKind":"re productwa","problemSoln":"re solutionwa","competitors":"re competitorwa","__v":0},{"_id":"569ca2ec2a0078bf67000001","productName":"Car Pooling","endUsers":"Car Pooling","problemsArea":"Car Pooling","productKind":"Car Pooling","problemSoln":"Car Pooling","competitors":"Car Pooling","__v":0},{"_id":"569ca75b81c7427376000001","productName":"Hola bhola","endUsers":"Hola bhola","problemsArea":"Hola bhola","productKind":"Hola bhola","problemSoln":"Hola bhola","competitors":"Hola bhola","__v":0},{"_id":"569ca7cee76ffc330d000001","productName":"Hola bhola","endUsers":"Hola bhola","problemsArea":"Hola bhola","productKind":"Hola bhola","problemSoln":"Hola bhola","competitors":"Hola bhola","__v":0},{"_id":"569ca8371fb843c816000001","productName":"ttereeter","endUsers":"ewweewkewkl","problemsArea":"kleklewklkl","productKind":"lklklewrkkl","problemSoln":"lkrlklkewkl","competitors":"lkklrlkerwlk","__v":0}]

but when I am trying to access any specific value with console.log(jsonData.productName) it gives me undefined on console

2
  • Maybe I'm being blind but I don't see any "productName" field in your json? Commented Feb 4, 2016 at 11:12
  • 1
    data is an array, you will have to iterate the array to access its elements or use a particular index to get the property value e.g console.log(jsonData[0]._id). Commented Feb 4, 2016 at 11:13

3 Answers 3

4

Fist of all, jsonData is an Array, so you need to access specific element first (i.e. jsonData[0]).

Also productName is only defined for some of the array elements, so it will still be undefined for jsonData[0].productName.

So try to find specific element of the array first, and then access its productName property.

Sign up to request clarification or add additional context in comments.

Comments

0

The response is basically an array of objects access it by using this

jsonData[0]._id

Comments

0
  • You are receiving an Array.

  • Only few of the objects in that array contains property 'productName'

So your code would be like :

for(var i=0 ;i<yourArray.length;i++){  'yourArray' is the response that you recieve
 var obj = yourArray[i];

 if(obj.productName){
   // handle code when Object has 'productName' property
 }else{
   // handle code when Object does not have 'productName' property
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.