点击单个按钮同时向MongoDB与MySQL写入数据,MySQL插入失败排查
问题排查及修复方案
已发现的核心错误点
- 拼写类错误
- 模型调用名拼写错误:定义的模型名为
userMysql,业务代码中写成了userMysl - 模型定义中多处
DataTypes拼写错误:存在String(首字母小写)、Datatypes(t小写)、DataType(少s)的错误写法,导致模型初始化失败 - 业务代码中存在语法断层:
console.log("Email---------->>>",email)行插入在find().then()中间,打断了Promise调用链,且email变量未定义
- 模型调用名拼写错误:定义的模型名为
- 字段不匹配问题
- 调用
create方法传入的字段名和模型定义的字段完全不匹配:比如模型中定义用户名为user_name、邮箱为user_email,传入的是username、email,未定义的字段不会被Sequelize写入数据库 - 查询邮箱时的条件字段错误:模型中邮箱字段为
user_email,查询条件写的是email,无法命中正确字段
- 调用
- 异步逻辑与响应问题
- 同时存在两处
res.send调用:MySQL写入完成和MongoDB查询完成各触发一次,先执行完的会先返回响应,后执行的异步操作会被中断,大概率MySQL写入还没完成就已经返回了响应 - 没有捕获异步错误:
create操作没有加catch回调,所有错误被吞,无法排查具体失败原因 - 冗余的原生MySQL连接:已经使用Sequelize管理连接,额外创建的原生MySQL连接完全没有使用,还会造成资源泄漏
- 同时存在两处
修正后代码
1. 模型文件修正
"use strict"; module.exports = function(sequelize, DataTypes) { var userMysql = sequelize.define("tce_users", { user_name: DataTypes.STRING(100), user_password: DataTypes.STRING, surname: DataTypes.STRING(100), user_email: DataTypes.STRING(100), user_firstName: DataTypes.STRING(100), user_lastname: DataTypes.STRING(100), // 修正DataTypes拼写 user_birthdate: DataTypes.STRING(17), user_birthplace: DataTypes.STRING(100), user_regnumber: DataTypes.STRING(100), user_ssn: DataTypes.STRING(100), user_level: DataTypes.STRING(100), user_verifycode: DataTypes.STRING(100), // 如需存储mother_name请补充对应字段,不需要则删除create方法里的对应参数 mother_name: DataTypes.STRING(100) }); return userMysql; };
2. 路由代码修正
const {Sequelize} = require('sequelize'); var path1 = require('path'); var root_path = path1.dirname(require.main.filename); var models = require(root_path+'/models'); // 改用async处理异步逻辑 router.post("/users", async function(req, res) { try { console.log("First In"); var user_id = req.query.user_id; var userData = []; var body_data = req.body.data; // 已删除冗余的原生MySQL连接代码,Sequelize会自动管理连接池 // MySQL写入逻辑 // 先查询重复邮箱,查询字段和模型定义保持一致 const existUser = await models.userMysql.findOne({ where: { user_email: body_data.userEmail.toLowerCase() } }); console.log("Email---------->>>", body_data.userEmail.toLowerCase()); if(!existUser) { // 写入字段和模型定义完全对应 await models.userMysql.create({ user_firstName: body_data.user_firstName, user_lastname: body_data.user_lastname, mother_name: body_data.motherName, surname: body_data.Surname, user_email: body_data.userEmail.toLowerCase(), user_name: body_data.user_name, user_password: body_data.user_password }); } // MongoDB查询逻辑 const users = await userModel.find({ "role": req.query.user_type }); users.forEach(user => { if(user.doc_id == ''){ var user = { id: user._id, name: user.fullName, status: user.status, role: user.role, email:user.email, lastLoginDate: user.lastLoginDate, lastLoginTime: user.lastLoginTime, flag:'0' } userData.push(user); } else { var usr = { id: user._id, name: user.fullName, picture: `${filelink}/api/usersData/download?document_id=${user.doc_id}`, status: user.status, role: user.role, email:user.email, lastLoginDate: user.lastLoginDate, lastLoginTime: user.lastLoginTime, flag:'1' } userData.push(usr); } }) // 所有操作完成后统一返回响应 res.send({ status: 200, data: userData }) } catch (error) { // 捕获所有错误打印,方便后续排查 console.log("操作失败:", error); res.status(500).send({ status: 500, msg: "操作失败", error: error.message }) } });
内容的提问来源于stack exchange,提问作者Anuj Todankar
相关产品推荐
相关产品推荐

