Как мне переместить функцию, связанную с MySQL, в файл contractsService.js?
Я получаю сообщение об ошибке:
C:\code\firstdrafte2e\app.js:8 app.get('/contracts', GetContracts(req, res)); ^
ReferenceError: req is not defined
контрактыService.js
exports = function GetContracts(req, res) {
new Promise(function (resolve, reject) {
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM " + config.DATABASE + ".Contracts", function (err, result, fields) {
if (err) throw err;
//console.info(result);
resolve(result);
});
});
}).then(rows => res.send(rows));
}
app.js
const express = require('express');
const app = express();
app.use(express.static('client'));
const config = require('./config')
var GetContracts = require('./contractsService');
app.get('/contracts', GetContracts());
module.exports = app;






Кажется, вы не передаете (req, res) своей функции GetContracts. Вы можете создать анонимную функцию, назначить ее переменной и экспортировать ее, как показано ниже.
контрактыService.js
exports.GetContracts = function(req, res) {
new Promise(function (resolve, reject) {
con.connect(function (err) {
if (err) throw err;
con.query("SELECT * FROM " + config.DATABASE + ".Contracts", function (err, result, fields) {
if (err) throw err;
//console.info(result);
resolve(result);
});
});
}).then(rows => res.send(rows));
}
App.js
const express = require('express');
const app = express();
app.use(express.static('client'));
const config = require('./config')
var GetContracts = require('./contractsService');
app.get('/contracts', GetContracts);
module.exports = app