我正在尝试执行具有next()的post函数;在代码的中间,所以我要从另一个文件导出函数的定义,并尝试从快速路由器调用它。但是它没有执行。

- 编辑

我已经按照上面的建议尝试过常规定义,但是在“ next();”时停止被调用并返回

Cannot POST /registerUser


index.js


[...]
//Definição e Chamada de Componentes

   const userLogin = require('./userControl/accessControl');
   const userRegister = require('./userControl/registerControl');

[...]

var router = express.Router();              // get an instance of the express Router


//Definição das Rotas
//==============================================================================
router.post('/login', userLogin.login);
router.post('/forgotPassword', userLogin.forgotPassword);
router.post('/registerUser', function(req, res, next) {userRegister.registerUser});


app.use('/', router);

// START THE SERVER
// =============================================================================
app.set('env', 'production');
app.listen(port);
console.log('API Listening on Port ' +port+'');


registerControl.js

exports.registerUser = function(req, res, next) {

    var query = "INSERT INTO [user] (name, email, phone, login, password, enabled, profile) VALUES ('example', '[email protected]', 'XXXXXXXXXXXX', 'ADMIN', '12345', 1, 'TEST')


    global.conn.request()
        .query(query)
        .then(result => {
            var userId = result.recordset[0].id;
            console.log(userId);
            if (result.rowsAffected[0] == 0) {
                res.sendStatus(500);
            }
            else {
                req.body.userId = userId;
                next();
               }

        })
        .catch(err => res.json(err));

}, function(req, res) {
    var query = "INSERT INTO [user_company] (company_id, user_id) VALUES ("+req.body.companyId+", "+req.body.userId+");"

    global.conn.request()
    .query(query)
    .then(result => {

        if (result.rowsAffected[0] == 0) {
            res.sendStatus(500);
        }
        else {
            res.sendStatus(200);
        }
    })
    .catch(err => res.json(err));

};


谢谢!

最佳答案

代替


  router.post('/registerUser', function(req, res, next) {userRegister.registerUser});


也许尝试


  router.post('/registerUser', userRegister.registerUser);


就像您在上述forgotPassword路线中所做的一样。

09-18 04:19