0

I'm trying to add the property 'board' (an object) for my model by using a function so that I can utilize multiple values from the request body to create the object.

Obj.create({
        name: req.body.name,
        admin: req.user.id.
        board: function(){
            var results = []
                rounds = req.body.rounds,
                teams = req.body.teams;

            for(x=0;x<rounds;x++){
                for(t=0;t<teams.length;t++){
                    results.push({
                        team: teams[t]
                    });
                }
            }
        }
    });

When I run this, I am given the exception of

Anchor does not support functions yet!

Seth
  • 659
  • 2
  • 7
  • 21
  • 1
    It's a good thing this isn't Jeopardy, since there is no question here. You are trying to do something, and the system is telling you that what you're trying to do isn't supported. Luckily, Farid was nice enough to just write some code for you below to do it. – sgress454 May 28 '14 at 05:51

1 Answers1

3

I think what you mean is this:

var results = []
    rounds = req.body.rounds,
    teams = req.body.teams;

for(x=0;x<rounds;x++){
    for(t=0;t<teams.length;t++){
        results.push({
            team: teams[t]
        });
    }
}

Obj.create({
    name: req.body.name,
    admin: req.user.id.
    board: results
});

Or this:

Obj.create({
    name: req.body.name,
    admin: req.user.id.
    board: (function(){ // immediately invoke the function
        var results = []
            rounds = req.body.rounds,
            teams = req.body.teams;

        for(x=0;x<rounds;x++){
            for(t=0;t<teams.length;t++){
                results.push({
                    team: teams[t]
                });
            }
        }
        return results;
    })()
});
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115