Your error reference is originating from the following line:
var jsondata = UrlFetchApp.fetch("http://api.grepwords.com/lookup?apikey=carter&q="+keyword);
In your code snippet above, this is the only instance where 'keyword' is used. Are you certain it is returning the correct information? Also, have you considered how your loop functions?
If your
var object = Utilities.jsonParse(jsondata.getContentText());
produces this output:
var object = [
{cpc: 'test1', cmp: 'test2', lms: 'test3', m1: 'test4'},
{cpc: 'test5', cmp: 'test6', lms: 'test7', m1: 'test8'},
{cpc: 'test9', cmp: 'test10', lms: 'test11', m1: 'test12'}
];
And if we use your loop as follows:
for (var i = 0; i < object.length; i++) {
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
Have you noticed that 'i' is never utilized in your loop? Additionally, you are repeatedly replacing values in results with the same data after each iteration. Is this truly your intended behavior?
function somefunc() {
var object = [
{cpc: 'test1', cmp: 'test2', lms: 'test3', m1: 'test4'},
{cpc: 'test5', cmp: 'test6', lms: 'test7', m1: 'test8'},
{cpc: 'test9', cmp: 'test10', lms: 'test11', m1: 'test12'}
];
var results = Array("Error", "Error", "Error", "Error");
if (object[0] != undefined)
{
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
for (var i = 0; i < object.length; i++) {
results[0] = object[0].cpc;
results[1] = object[0].cmp;
results[2] = object[0].lms;
results[3] = object[0].m1;
}
return results;
}
console.log(somefunc());