使用嵌套循环查询返回响应

时间:2015-04-17 14:16:26

标签: node.js sequelize.js

我在我的addAll方法中循环并插入元素。一旦完成,我想回复一个回复。我的代码目前有效,但我不确定如何让响应发生。

路线

.post(function (req, res) {
    var screening_module = Screening_module.build();

    screening_module.screening_module = req.body.module;
    screening_module.screening_module.organization_id = req.user.organization_id;
    screening_module.addAll(function (success) {
            res.json({message: 'screening_module created!'});
        },
        function (err) {
            res.status(err).send(err);
        }); });

的addAll

addAll: function (onSuccess, onError) {
    var screening_module = this.screening_module;
    screening_module.selectedUser.forEach(function (user) {
        Screening_module.create({
            organization_id: screening_module.organization_id,
            supervisor: screening_module.supervisor.id,
            name: screening_module.name,
            description: screening_module.description,
            deadline: screening_module.deadline,
            category_id: screening_module.category.id,
            screening_module_type_id: screening_module.type.id,
            user_id: user.id
        }).then(function (createdScreeningModule) {
            Screening.create({
                user_id: createdScreeningModule.dataValues.user_id,
                screening_module_id: createdScreeningModule[null]
            }).then(function (createdScreening) {
                screening_module.selectedSkillsets.forEach(function (skillset) {
                    Screening_has_skillset.create({
                        screening_id: createdScreening[null],
                        skillset_id: skillset.id
                    })
                })
            })
        })
    });
},

1 个答案:

答案 0 :(得分:0)

Promise是你的朋友

var Promise = require("bluebird");

addAll: function (onSuccess, onError) {
    var screening_module = this.screening_module;
    Promise.each(screening_module.selectedUser, function (user) {
        return Screening_module.create({
            organization_id: screening_module.organization_id,
            supervisor: screening_module.supervisor.id,
            name: screening_module.name,
            description: screening_module.description,
            deadline: screening_module.deadline,
            category_id: screening_module.category.id,
            screening_module_type_id: screening_module.type.id,
            user_id: user.id
        }).then(function (createdScreeningModule) {
            return Screening.create({
                user_id: createdScreeningModule.dataValues.user_id,
                screening_module_id: createdScreeningModule[null]
            }).then(function (createdScreening) {
                return Promise.each(screening_module.selectedSkillsets, function (skillset) {
                    return Screening_has_skillset.create({
                        screening_id: createdScreening[null],
                        skillset_id: skillset.id
                    });
                });
            });
        });
    }).then(onSuccess, onError);
},

未经测试,但希望能让您了解如何处理。 IMO,这种深度嵌套是code smell,您可能需要再次考虑设计