Jackthomson
Jackthomson

Reputation: 644

Node Controller - Async pass anonymous function

Trying to create a controller using async but I cannot pass an anonymous function as the third param. I keep getting a parsing error of unexpected token { - 'Any ideas? The error goes away if I just pass in function(err, responses) inside the params directly. I'm basically trying to iterate over two object that will be returned, find a contract name for each and then assign an array of data which is assigned for that contract.

var request = require('request'),
helpers = require('../../helpers.js'),
async = require('async');

module.exports.getStatementBreakdown = function(req, res) {
    var httpGet,
    response,
    urls = [
        '/financial-advances/',
        '/financial-adjustments/'
    ];

    httpGet = function(url, callback) {
        var options = helpers.buildAPIRequestOptions(req, url);
        request(options,
            function(err, res, body) {
                var data = {};
                if(!err && res.statusCode === 200) {
                    data = JSON.parse(body);
                }
                callback(err, data);
            }
        );
    };

    response = function(err, responses) {}

    async.map(urls, httpGet, response) {
        var statementBreakdown = {},
        response,
        breakdown,
        i,
        j,
        contractName,
        key;

        for(i = 0; i < responses.length; i++) {
            response = responses[i];
            for(key in response) {
                if(key !== 'meta' || key !== 'notifications') {
                    breakdown = response[key];
                    for(j = 0; j < breakdown.length; j++) {
                        contractName = breakdown[j].reimbursementContract.name;
                    }
                }
            }
        }
        statementBreakdown[contractName] = [];
        statementBreakdown[contractName].push(breakdown);
        res.send(statementBreakdown);
    });
};

Upvotes: 0

Views: 87

Answers (1)

Zachary Jacobi
Zachary Jacobi

Reputation: 1073

Based on the code sample you posted, you're getting an unexpected token because you have a curly brace in the wrong place.

See here: async.map(urls, httpGet, response) {? That curly brace is the unexpected token.

Javascript gives an unexpected token when there is a character where it shouldn't be. In this case, you've added a curly brace right after a function call. Curly braces are expected after control flow statements and function declarations.

I'm not sure what exactly you're going for, but maybe something like this?

async.map(urls, httpGet, function(err, responses) {
        var statementBreakdown = {},
        response,
        breakdown,
        i,
        j,
        contractName,
        key;

        for(i = 0; i < responses.length; i++) {
            response = responses[i];
            for(key in response) {
                if(key !== 'meta' || key !== 'notifications') {
                    breakdown = response[key];
                    for(j = 0; j < breakdown.length; j++) {
                        contractName = breakdown[j].reimbursementContract.name;
                    }
                }
            }
        }
        statementBreakdown[contractName] = [];
        statementBreakdown[contractName].push(breakdown);
        res.send(statementBreakdown);
    });
});

For further help with async.map, see the docs.

Upvotes: 2

Related Questions