回调地狱,厄运金字塔,node.js

时间:2017-02-05 11:27:10

标签: javascript node.js mongodb express promise

我是节点的新手,在我完成我的API之后,我意识到所有这些都是一团糟,并且是一个回调地狱,这迫使我学习了承诺,现在一切都很好,直到我面对一个API条件超过可能的函数,我的问题是如何做嵌套的promises,我的代码是关于一个父对象谁有一个用户附加到它,当调用删除路由有很多条件:

  1. 如果此父级附加了子级,则不应删除
  2. 没有孩子

    一个。这个父母在多个学校,school_id从父对象的学校ID和附加到它的用户对象中删除

    湾如果这个父母没有孩子而且只在school_id上,那么它应该被删除,并且也应该删除附加的用户

  3. 代码看起来像这样,它的功能是100%

    router.post('/delete',Validation, function (req, res) {
    var school_id = req.body.schoolId;
    var parent_id = req.body.selected[0];
    /
    Student.findOne({parent_ids:parent_id},function(err,parentF){
        if(err){
            console.log(err);
            res.json({status:"error",message:"an error occurred"});
            return
        }else if(parentF){
            res.json({status:"error", message:"you can not delete a parent who has students associated with it"});
            return;
        }else{
            Parent.findOne({_id:parent_id},function(err,parent){
                if(err){
                    console.log(err);
                    res.json({status:"error",message:"an error occurred"});
                    return;
                }else{
                    if(parent.school_id.length>1){
                        var a = parent.school_id.indexOf(school_id);
                        parent.school_id.pop(a);
                        parent.save(function(err,parent){
                            if(err){
                                console.log(err);
                                res.json({status:"error",message:"an error occurred"});
                                return;
                            }else{
                                User.findOne({refid:parent_id},function(err,user){
                                    if(err){
                                        console.log(err);
                                        res.json({status:"error",message:"an error occurred"});
                                        return;
                                    }else {
                                        user.school_id.pop(a);
                                        user.save(function(err) {
                                            if(err){
                                                console.log(err);
                                                res.json({status:"error",message:"an error occurred"});
                                                return;
                                            }else{
                                                res.json({status: "success", message: "parent removed"});
                                                return;
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }else{
                        Parent.remove({_id: parent_id}, function (err) {
                            if (err) {
                                res.json({status: "error", message: err.message});
                            } else {
                                User.remove({refid:parent_id},function(err){
                                    if (err) {
                                        res.json({status: "error", message: "parent user wasn't deleted"});
                                        return;
                                    }else{
                                        res.json({status: "success", message: "parent data successfully deleted"});
                                        return;
                                    }
                                });
    
                            }
                        });
                    }
                }
            });
        }
    });
    });
    

    我正在尝试应用承诺并使我的代码更好。

    对于这个冗长的问题感到抱歉,但我已经碰壁了任何建议表示赞赏

1 个答案:

答案 0 :(得分:2)

创造一些"承诺" findOne的调味函数,删除并保存

let findOneP = (object, params) => new Promise((resolve, reject) => {
    object.findOne(params, (err, data) => {
        if (err) {
            return reject(err);
        }
        resolve(data);
    });
});
let removeP = (object, params) => new Promise((resolve, reject) => {
    object.remove(params, (err, data) => {
        if (err) {
            return reject(err);
        }
        resolve(data);
    });
});
let saveP = (object) => new Promise((resolve, reject) => {
    object.save((err, data) => {
        if (err) {
            return reject(err);
        }
        resolve(data);
    });
});

使用Bluebird Promisify可以更轻松地完成上述工作,但是由于您还没有指定蓝鸟,我只是使用了一个快速而肮脏的承诺包装

然后使用承诺链让生活更轻松

router.post('/delete',Validation, function (req, res) {
    var school_id = req.body.schoolId;
    var parent_id = req.body.selected[0];

    findOneP(Student, {parent_ids: parent_id})
    .then(parentF => {
        if (parentF) {
            throw "you can not delete a parent who has students associated with it";
        }
    })
    .then(() => findOneP(Parent, {_id: parent_id}))
    .then(parent => {
        if (parent.school_id.length > 1) {
            var a = parent.school_id.indexOf(school_id);
            parent.school_id.pop(a);
            return saveP(parent)
                .then(parent => findOneP(User, {refid: parent_id}))
                .then(user => saveP(user))
                .then(() => "parent removed");
        }
        return removeP(Parent,{_id: parent_id})
            .then(() => removeP(User, {refid: parent_id}))
            .then(() => "parent data successfully deleted");
    })
    .then(message => res.json({status: "success",message}))
    .catch(message => res.json({status: "error",message}));
});

我刚刚读到您使用节点4.2 - 您可能需要

  1. 通过像babel
  2. 这样的转录程序运行
  3. 加载Promise库,例如bluebird
  4. 如果使用蓝鸟,请使用它的promisify功能,而不是"手动"宣传