【问题标题】:Sequlize callback when using transaction使用事务时的 Sequelize 回调
【发布时间】:2016-03-09 22:42:55
【问题描述】:

在我的应用程序中,我有一个模型说它是Record,而一个Record 可能有几个Attachment 可以上传到服务器。

一般情况下,在创建RecordAttachment(files)时,我会先上传并保存文件,然后保存记录,如下所示:

function createOrUpdateInfo(req, res, next) {
    var record = req.body;
    var attachmentIds = (record.files || []).map(function (a) {
        return a.id;
    });
    var attachmentFilter = {
        where: {
            id: {
                $in: attachmentIds || []
            }
        }
    };
    DB.sequelize.transaction(function (t) {
        var pro;
        if (record.id) {
            //update

            //update the basic information first
            return Record.update(record, {
                where: {
                    id: req.params.id
                }, transaction: t
            }).then(function (num, infos) {

                //find the record just saved.
                return Record.findById(req.params.id).then(function (record) {

                    //find the attachmens which have been saved
                    return Attachment.findAll(attachmentFilter).then(function (atts) {

                        //update the record, create the association.
                        return record.setFiles(atts, {transaction: t});
                    });
                })
            });
        } else {

            //save
            return Record.create(record, {transaction: t}).then(function (record) {
                return Attachment.findAll(attachmentFilter).then(function (atts) {
                    return record.setFiles(atts, {transaction: t});
                });
            });
        }

    }).then(function (result) {
        Util.sendJson(res, result)
    }).catch(function (err) {
        next({message: err.message, code: 500});
    });
}

如图所示,创建或更新Record时嵌套回调过多。

这可以解决吗?

【问题讨论】:

    标签: node.js orm sequelize.js


    【解决方案1】:

    我对您的代码进行了一些重组。希望对您有所帮助。

    function createOrUpdateInfo(req, res, next) {
        var record = req.body;
        var attachmentIds = (record.files || []).map(function (a) {
            return a.id;
        });
        var attachmentFilter = {
            where: {
                id: {
                    $in: attachmentIds || []
                }
            }
        };
        DB.sequelize.transaction(function (t) {
            var pro;
            return (function () {
                if (record.id) {
                    //update
                    //update the basic information first
                    return Record.update(record, {
                        where: {
                            id: req.params.id
                        }, transaction: t
                    }).then(function (num, infos) {
                        //find the record just saved.
                        return Record.findById(req.params.id);
                    });
                } else {
                    //save
                    return Record.create(record, {transaction: t});
                }
            }()).then(function () {
                //find the attachmens which have been saved
                return Attachment.findAll(attachmentFilter);
            }).then(function (atts) {
                //update the record, create the association.
                return record.setFiles(atts, {transaction: t});
            });
    
        }).then(function (result) {
            Util.sendJson(res, result)
        }).catch(function (err) {
            next({message: err.message, code: 500});
        });
    }
    

    【讨论】:

    • 你能解释一下吗?只是Promise synatx?
    猜你喜欢
    • 1970-01-01
    • 2023-02-23
    • 2020-09-24
    • 2015-06-03
    • 2019-09-24
    • 1970-01-01
    • 2016-04-21
    • 2022-12-18
    • 1970-01-01
    相关资源
    最近更新 更多