I have a program that has to loop through an array and then post it and getback the data. for example.
The array is as below.
var fruits = ["apple","banana","mango","orange","carrot"]
I have an url as below.
var url ="https://www.fruitsdatabase.com/fruitNames"
and I want to post the data. And I'm using the below code.
for (var i in fruits) {
var uhri = "https://www.fruitsdatabase.com/fruitNames";
var options = {
uri: uhri,
method: 'POST',
json: {
"type": "fruits",
"params": {
"name": fruits[i]
}
}
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
if (body) {
console.log(fruits[i] + "\t" + body);
} else {
console.log('sorry no results found');
}
} else {
console.log("Wow this is an err");
}
});
}
When I'm trying to print this the result that I get looks like this.
apple [object, object]
banana [object, object]
mango [object, object]
orange [object, object]
carrot [object, object]
when I change console.log(fruits[i] + "\t" + body);
to console.log(body);
{
Table1:
[ { Name: 'apple',
type: 'fruit',
color: 'green',
}
]
}
{
Table1:
[ { Name: 'banana',
type: 'fruit',
color: 'yellow',
}
]
}
{
Table1:
[ { Name: 'mango',
type: 'fruit',
color: 'yellow',
}
]
}
{
Table1:
[ { Name: 'orange',
type: 'fruit',
color: 'orange',
}
]
}
{}
since carrot is not a fruit, It should return a {}
and in my above code instead of printing carrot [object, object]
, it should print carrot sorry no results found
. please let me know where am I going wrong and how can I fix it.
Thanks
via user3872094
No comments:
Post a Comment