使用函数创建模型属性

时间:2014-05-28 03:04:06

标签: node.js sails.js waterline

我试图添加该物业' board' (我的模型的一个对象)通过使用一个函数,以便我可以利用请求体中的多个值来创建对象。

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]
                    });
                }
            }
        }
    });

当我运行时,我被视为

的例外
  

Anchor还不支持函数!

1 个答案:

答案 0 :(得分:3)

我认为你的意思是:

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
});

或者这个:

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;
    })()
});