0

I keep running to Cannot find function for each in object error while trying to loop entries. Is there something I am not seeing?. Here the code. This code is supposed to fetch time data from a system via API do calculations and send email

function getTime() {
    var range = [5323, 9626, 4998];
    var user = [];
    for (var i = 0; i < range.length; i++) {

        var auth = 'token'

        var from = '2020-01-08'
        var to = '2020-01-09'
        var url = 'https://api.10000ft.com/api/v1/users/' + range[i] + '/time_entries?from=' + from + '&to=' + to + '&auth=' + auth;

        var options = {
            method: 'get',
            headers: {
                Authorization: 'Bearer ' + auth
            }
        };

        var submitted_time_entries = {};
        var response = UrlFetchApp.fetch(url, options);
        var response = JSON.parse(response.getContentText());
        var time_entries = response.data;

        time_entries.forEach(function (time_entry) {
            if (time_entry.user_id in submitted_time_entries) {
                submitted_time_entries[time_entry.user_id] += time_entry.hours;
            } else {
                submitted_time_entries[time_entry.user_id] = time_entry.hours;
            }
        });

        submitted_time_entries.forEach(function (user_id) {
            if (submitted_time_entries[user_id] < 3) {
                //send mail
            }
        });
    }
}
2
  • forEach() not foreach(), note the camel casing. Commented Jan 17, 2020 at 18:13
  • @ross Thanks for flagging this. I have changed the casing but still, get an error Cannot find function forEach in object [object Object]. Look at my edited code. Commented Jan 17, 2020 at 18:18

2 Answers 2

1

response.data is probably not the array you expect. The server may be returning an error or a successful response that isn't parseable as an array. To find out, print response.data to the console and confirm it's the array you expect.

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

3 Comments

Hey @spork thanks for the input. I used Object.keys() method that returns an array of the keys of an object and it worked. This is how that part of the code looks like now: Object.keys(time_entries).forEach(function (time_entry) {
Ok, so it sounds like your API returns an object instead of an array, which is why you were getting the error.
It appears so. Thanks for taking the time to look at the code
1

Seems my API returned an object. I figured out the way around it by using Object.keys method and it worked. Here is the working code.

function getTime() {
    var range = [53, 926, 8098];
    var user = [];
    for (var i = 0; i < range.length; i++) {

        var auth = 'token';

        var from = '2020-01-08'
        var to = '2020-01-09'
        var url = 'https://api.10000ft.com/api/v1/users/' + '449625' + '/time_entries?from=' + from + '&to=' + to + '&auth=' + auth;

        var options = {
            method: 'get',
            headers: {
                Authorization: 'Bearer ' + auth
            }
        };

        var submitted_time_entries = {};
        var response = UrlFetchApp.fetch(url, options);
        var response = JSON.parse(response.getContentText());
        var time_entries = response.data;


        Object.keys(time_entries).forEach(function (time_entry) {
            if (time_entry.user_id in submitted_time_entries) {
                submitted_time_entries[time_entry.user_id] += time_entry.hours;
            } else {
                submitted_time_entries[time_entry.user_id] = time_entry.hours;
            }
        });

        Object.keys(submitted_time_entries).forEach(function (user_id) {
            if (submitted_time_entries[user_id] < 3) {
                Logger.log(time_entry)
                //send mail
            }
        });
    }
}

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.