数据库插入后返回acc_type与accnumber及SQL查询语法错误求助
账户接口问题修复指南
一、修复/api/create-account接口返回指定字段的问题
当前代码返回的是数据库插入操作的默认结果(如受影响行数),而非需求中的acc_type和accnumber。由于这两个值在代码中已提前生成,直接将其放入响应数据即可,无需额外查询数据库。
修改后的代码:
app.post('/api/create-account', function (req, res) { var prologue = '00345'; var digits = Math.floor(Math.random() * 900000) + 100000; var accnumber = prologue + digits; var firstname = req.body.firstname; var lastname = req.body.lastname; var fullname = firstname + ' ' + lastname; var acc_type = req.body.acc_type; var address = req.body.address; var telephone = req.body.telephone; var email = req.body.email; var birth_date = req.body.birth_date; var gender = req.body.gender; var bvn_number = req.body.bvn_number; dbConn.query('insert into fdsh_demobank (firstname, lastname, address, telephone, email,birth_date, gender,bvn_number, acc_type,accnumber ) values (?,?,?,?,?,?,?,?,?,?)', [firstname, lastname, address, telephone, email, birth_date, gender, bvn_number, acc_type, accnumber], function (error, results, fields) { if (error) throw error; // 返回指定的账户类型和账号 return res.status(200).send({ error: false, data: { acc_type, accnumber }, message: '账户创建成功' }); }); sendConfirmationEmail(fullname, accnumber, acc_type, email); sendOnboardingEmail(fullname, email); })
二、修复/api/get-customer/:email接口的SQL语法错误
SQL语句中email is = ?属于语法错误,is关键字仅用于判断NULL(如is null),普通等值判断直接使用=即可。
修改后的代码:
app.get('/api/get-customer/:email', function (req, res) { var email = req.params.email; // 修正SQL语法错误 dbConn.query('select * from fsdh_demobank where email = ?', [email], function (error, results, fields) { if (error) throw error; return res.status(200).send({ error: false, data: results[0], message: '客户数据获取成功' }); }) })
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

