Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如果操作请求失败,如何回滚redux状态和数据库_Node.js_Reactjs_Express_Redux_Sequelize.js - Fatal编程技术网

Node.js 如果操作请求失败,如何回滚redux状态和数据库

Node.js 如果操作请求失败,如何回滚redux状态和数据库,node.js,reactjs,express,redux,sequelize.js,Node.js,Reactjs,Express,Redux,Sequelize.js,我有一个提交按钮,它将发送一个Redux操作和三个axios请求。第一个是创建班级,第二个是添加班主任,最后一个是添加班级学生。教师表有classId外键,它依赖于创建类的返回值。学生有teachId外键参照教师表 当我单击submit按钮时,在ButtonOnClick操作中,它将发送Redux操作。如果任何请求失败,我如何处理和回滚Redux状态和数据库 对于Fronded,我的代码如下 export const finalizeClassSetting = ({ className, te

我有一个提交按钮,它将发送一个Redux操作和三个axios请求。第一个是
创建班级
,第二个是
添加班主任
,最后一个是
添加班级学生
。教师表有
classId
外键,它依赖于创建类的返回值。学生有
teachId
外键参照教师表

当我单击submit按钮时,在ButtonOnClick操作中,它将发送Redux操作。如果任何请求失败,我如何处理和回滚Redux状态和数据库

对于Fronded,我的代码如下

export const finalizeClassSetting = ({ className, teacherName, StudentsDetail }) => async (dispatch) => {
    dispatch({ type: CLASS_REQUEST })
    try {
        //create new class
        resultClass = createClass(className)
        dispatch({ type: CREATE_CLASS, payload: resultClass })
        const classId = resultClass.classId
        try {
            dispatch({ type: TEACHER_REQUEST })
            resultTeacher = createTeacher(teacherName,classId)
            dispatch({ type: CREATE_TEACHER, payload: resultTeacher})
            const teacherId = resultTeacher.teacherId
            try {
                dispatch({type:STUDENT_REQUEST})
                resultStudent = createStudents(classId, teacherId, studentsDetail)
                dispatch({ type: CREATE_STUDENT, payload:resultStudent })
            } catch (error) {
                dispatch({ type: STUDENT_REQUEST_FAIL, payload: error })
            }
        } catch (error) {
            dispatch({ type: TEACHER_REQUEST_FAIL, payload: error})
        }
    } catch (error) {
        dispatch({ type: CLASS_REQUEST_FAIL, payload: error})
    }
}
createClass
createTeacher
createStudents
将调用后端控制器创建一个新类,新教师和新学生分开。它使用Sequalize。就像

exports.createXXX = async (req, res) => {
    ...
    try {
        const resourceFound = await Model.findOne({
            where: { condition }
        })
        if (resourceFound) {
            return res.status(400).send({ status: 1, data: { message: "resource has existing" } })
        }
        const newResource = await Model.create({ xxxx })
        return res.status(200).send({ status: 0, data: { newResource: newResource, message: "create new resource successful" } })
        ...
    }
    catch(error) {
        return res.status(500).send({ status: 1, data: { message: "server error"}})
    }
}
使用上面我的当前代码,如果请求中有任何错误,则保存到数据库中的数据库已经无法回滚