Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Empty Array Causes Undefined Error #237

Closed
rhythmicdevil opened this Issue · 2 comments

3 participants

@rhythmicdevil

I have an array of objects where each object has a key of 'criteria' has an array assigned to it of 0 or more items. Async.each always throws an error when it gets to the empty feature.criteria array. Not sure if this is intended or not. I expected that Async would simply skip the array.

Features is a JSON file

[
    {
        "key": "flags",
        "type": "number",
        "criteria": [
            {
                "gt": 0
            },
            {
                "lt": 10
            }
        ],
        "actions": [
            {
                "aggregate": {
                    "coll": "some_collection"
                }
            }
        ]
    },
    {
        "key": "rpr",
        "type": "number",
        "criteria": [
            {
                "gt": 0
            },
            {
                "lt": 3
            }
        ],
        "actions": [
            {
                "aggregate": {
                    "coll": "some_collection"
                }
            }
        ]
    },
    {
        "key": "sn",
        "type": "string",
        "criteria": [],
        "actions": [
            {
                "aggregate": {
                    "coll": "some_collection"
                }
            }
        ]
    }
]

Test Script that Errors

Async = require('async');
Features = require('./config/features');
Util = require('util');

Async.each(Features, 
    function(feature, callback){
        console.log('Feature: ' + Util.inspect(feature));

        Async.each(feature.criteria, 
            function(crit){
                console.log('Crit: ' + Util.inspect(crit));
            }, 
            function(err){
                console.log('Criteria Error: ' + Util.inspect(err));    
            }
        );


        callback(null);
    }, 
    function(err){
        console.log('Feature Error: ' + Util.inspect(err));
    }
);

Test Script that does not Error

Async = require('async');
Features = require('./config/features');
Util = require('util');

Features.forEach(function(feature){
    console.log('Feature: ' + Util.inspect(feature));

    feature.criteria.forEach(function(crit){
        console.log('Crit: ' + Util.inspect(crit));
    });

});
@brianmaissy

I can't seem to replicate the problem. When I run your 'Test Script that Errors', it runs properly, and I get this output:

Feature: { key: 'flags',
  type: 'number',
  criteria: [ { gt: 0 }, { lt: 10 } ],
  actions: [ { aggregate: [Object] } ] }
Crit: { gt: 0 }
Crit: { lt: 10 }
Feature: { key: 'rpr',
  type: 'number',
  criteria: [ { gt: 0 }, { lt: 3 } ],
  actions: [ { aggregate: [Object] } ] }
Crit: { gt: 0 }
Crit: { lt: 3 }
Feature: { key: 'sn',
  type: 'string',
  criteria: [],
  actions: [ { aggregate: [Object] } ] }
Criteria Error: undefined
Feature Error: null
@caolan
Owner

@rhythmicdevil Any update on this?

@caolan caolan closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.