I want to ask something which is actually I don't know what's wrong with my code. So, i've docs from database. Here is my docs :
{
"docs":[
{
"orderDate":{
"value":"2018-03-20",
"type":"date"
},
"name":{
"value":"Abid Rakhmansyah",
"type":"text"
},
"phone":{
"value":"082117414233",
"type":"number"
},
"email":{
"value":"[email protected]",
"type":"email"
},
"paid":{
"value":true,
"type":"boolean"
}
},
{
"orderDate":{
"type":"date",
"value":"2018-03-13T05:14:00.806Z"
},
"name":{
"value":"Iqbal Maulana",
"type":"text"
},
"phone":{
"value":"082117414233",
"type":"number"
},
"email":{
"value":"[email protected]",
"type":"email"
},
"paid":{
"type":"boolean",
"value":false
}
},
{
"orderDate":{
"value":"2018-03-20",
"type":"date"
},
"name":{
"value":"Abdullah",
"type":"text"
},
"phone":{
"value":"092034",
"type":"number"
},
"email":{
"value":"[email protected]",
"type":"email"
},
"paid":{
"value":true,
"type":"boolean"
}
},
{
"orderDate":{
"value":"2018-03-20",
"type":"date"
},
"name":{
"value":"asd",
"type":"text"
},
"phone":{
"value":"234234",
"type":"number"
},
"email":{
"value":"[email protected]",
"type":"email"
},
"paid":{
"value":true,
"type":"boolean"
}
},
{
"orderDate":{
"type":"date",
"value":"2018-03-20T06:01:54.821Z"
},
"name":{
"value":"as",
"type":"text"
},
"phone":{
"value":"082117414233",
"type":"number"
},
"email":{
"value":"[email protected]",
"type":"email"
},
"paid":{
"type":"boolean",
"value":false
}
}
],
"total":5,
"limit":8,
"page":1,
"pages":1
}
I want to get only value property in the objects. So, i try to code like this.
orders.docs.forEach( function (arrayItem)
{
Object.keys(arrayItem.toJSON()).forEach(function(key) {
console.log(key, JSON.stringify(arrayItem[key].value));
});
});
I think that it will be work. But, I don't know why... The result of my code doesn't show like what I expected. Here is the result of my code :
[0] orderDate "2018-03-20"
[0] (node:20944) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'value' of undefined
For the first loop it work fine, but when it going to 2nd loop. The value is undefined. What's is wrong?
toJSONturn the object to JSON. As in, a string. In that case, what are you forEach-ing on? And why would the "JSON keys" (as that's what you are forEaching, whatever that is) work on the base item?Object.keyson it won't actually give you the keys from the object.$objectToArrayoperator to get the values