简体   繁体   English

使用函数创建模型属性

[英]Creating Model Properties using Function

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. 我试图通过使用函数为模型添加属性“ 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]
                    });
                }
            }
        }
    });

When I run this, I am given the exception of 当我运行此程序时,我得到的例外

Anchor does not support functions yet! 锚点尚不支持功能!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM