首页 > 解决方案 > 未返回 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();
    })
})
}

我试过使用回调,但它不起作用。我想是因为我不明白如何使用它

请帮我

标签: node.jsexpress

解决方案


您需要将要输出的数据传递给方法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);
    })
})
}

推荐阅读