node.js - 使用事务时的 Sequelize 回调

标签 node.js orm sequelize.js

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

通常,当使用附件(文件)创建记录时,我会先上传并保存文件,然后保存记录,如下所示:

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

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

这个问题可以解决吗?

最佳答案

我稍微重新组织了你的代码。希望对您有所帮助。

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

关于node.js - 使用事务时的 Sequelize 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35892739/

相关文章:

java - MyBatis:Sybase 存储过程返回零行

node.js - 如何使用sequelize获取表格列表

javascript - 创建n :m objects using json and sequelize?

node.js - TypeError : user. findOne 不是我的模型上的函数

javascript - Node js错误: spawn ENOENT

javascript - readstream在nodejs中将文件存储在哪里

javascript - 重定向后快速 session 不持久

javascript - NodeJS 在正确的时间获取变量的问题 [npm like module]

java - 子表上的 Hibernate Criteria API

java - 为 Eclipse 安装了 Hibernate Tools 插件,但无法访问代码生成功能