node.js - 未返回 NodeJS 请求
问题描述
这是源代码
API.js
router.post('/curriculum2_section/', (req,res)=> {
curriculum2_section.CREATE(req.body.curr2_section,req.body.curr2_id,req.body.curr2_section_student_amount)
})
课程 2_section.js
module.exports.CREATE = function CREATE(curr2_section,curr2_id,curr2_section_student_amount) {
var sqlString = `INSERT INTO curriculum2_section(curr2_id,curr2_section,curr2_section_student_amount)VALUES('${curr2_id}',${curr2_section},${curr2_section_student_amount})`
pool.getConnection((err, conn) => {
if(err) throw err
pool.query(sqlString, function (err, rows) {
console.log('Query String:\n '+this.sql);
if(err) throw err
console.log('Data Insert: '+curr2_section+','+curr2_id+','+curr2_section_student_amount)
conn.release();
})
})
}
我试过使用回调,但它不起作用。我想是因为我不明白如何使用它
请帮我
解决方案
您需要将要输出的数据传递给方法res
,以便从 Express 中获取输出。
鉴于您在这里所拥有的,您最好的选择可能是添加一个回调作为 的最后一个参数,curriculum2_section.CREATE
并使用该回调将数据传递给res
.
https://expressjs.com/en/4x/api.html#res
router.post('/curriculum2_section/', (req,res)=> {
curriculum2_section.CREATE(req.body.curr2_section,req.body.curr2_id,req.body.curr2_section_student_amount, (err, data) => {
res.send(data);
});
});
module.exports.CREATE = function CREATE(curr2_section,curr2_id,curr2_section_student_amount, cb) {
var sqlString = `INSERT INTO curriculum2_section(curr2_id,curr2_section,curr2_section_student_amount)VALUES('${curr2_id}',${curr2_section},${curr2_section_student_amount})`
pool.getConnection((err, conn) => {
if(err) throw err
pool.query(sqlString, function (err, rows) {
console.log('Query String:\n '+this.sql);
if(err) throw err
console.log('Data Insert: '+curr2_section+','+curr2_id+','+curr2_section_student_amount)
conn.release();
cb(null, 'Data Insert: '+curr2_section+','+curr2_id+','+curr2_section_student_amount);
})
})
}
推荐阅读
- ruby-on-rails - 从 ruby 的下拉列表中获取选定的选项
- python - python中的剥离函数/打印-为什么最后删除空格没有区别
- c# - Azure Functions 代理身份验证
- python - Django ORM:带有后续过滤的窗口函数
- javascript - 为什么这个嵌套的 ng-include 不会显示结果
- r - R - 错误:is.character(location) 不是 TRUE
- java - 任何操作系统设置都会影响 ExecutorService 吗?
- mysql - 如何计算特定日期范围的期初和期末股票
- graphql - graphql,当有“添加”和“更新”突变时如何设计输入类型?
- symfony - Symfony/Doctrine - QueryException - [语义错误] 第 0 行,'pizza-mia' 附近的第 48 列:错误:'pizza' 未定义