Guest User

Untitled

a guest
Dec 27th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. Cannot read property 'end' of undefined
  2. at ServerResponse.<anonymous> (/usr/my_server/app.js:45:24)
  3.  
  4. var express = require('express');
  5. var path = require('path');
  6. var logger = require('morgan');
  7. var cookieParser = require('cookie-parser');
  8. var bodyParser = require('body-parser');
  9. var mysql= require('mysql2');
  10. var http = require('http');
  11. var app = express();
  12.  
  13. var addReferFriend = require('./addReferFriend');
  14.  
  15. app.set('views', path.join(__dirname, 'views'));
  16. app.set('view engine', 'ejs');
  17.  
  18. app.use(bodyParser.json());
  19. app.use(bodyParser.urlencoded({ extended: false }));
  20. app.use(cookieParser());
  21. app.use(express.static(path.join(__dirname, 'public')));
  22.  
  23.  
  24. app.use(async function(req, res, next) {
  25. try {
  26. if( req.dbConnection ) {
  27. // ensure that req.dbConnection was not set already by another middleware
  28. throw new Error('req.dbConnection was already set')
  29. }
  30.  
  31. let connection = mysql.createConnection({
  32. host: 'xx',
  33. user: 'xx',
  34. password: 'xx',
  35. database: 'xx'
  36. });
  37.  
  38. res.on("finish", function() {
  39. // end the connection after the resonponse was send
  40. req.dbConnection.end()
  41. });
  42.  
  43. // wait for the connection and assign it to the request
  44. req.dbConnection = await connection.connect();
  45. next();
  46. } catch(err) {
  47. next(err);
  48. }
  49. });
  50.  
  51. app.use('/api/addReferFriend', addReferFriend);
  52.  
  53. // catch 404 and forward to error handler
  54. app.use(function(req, res, next) {
  55. var err = new Error('Not Found');
  56. err.status = 404;
  57. next(err);
  58. });
  59.  
  60. module.exports = app;
  61. var server = http.createServer(app);
  62. server.listen(3955);
  63.  
  64. var express = require('express');
  65. var router = express.Router();
  66.  
  67. /* GET users listing. */
  68. router.post('/', function(req, res, next) {
  69. var uid = req.body.uid;
  70. var friendReferCode = req.body.friendReferCode;
  71.  
  72. var sqlCheckIfExist = "SELECT my_refer FROM hub_user WHERE my_refer = '" + friendReferCode + "'";
  73. var sqlCodeCheckSameAsMine = "SELECT my_refer FROM hub_user WHERE uid = '" + uid + "'";
  74.  
  75. function checkIfUserCodeExist() {
  76. return req.dbConnection.query(sqlCheckIfExist)
  77. .then(([rows, fields]) => {
  78. if (rows == 0) {
  79. console.log("Non esiste!")
  80.  
  81. return res.send(JSON.stringify({
  82. "status": 500,
  83. "response": "codeNotExist"
  84. }));
  85. }
  86. console.log("Esiste!")
  87. return checkIfCodeIsSameAsMine(connection)
  88. })
  89. }
  90.  
  91. function checkIfCodeIsSameAsMine() {
  92. return req.dbConnection.query(sqlCodeCheckSameAsMine)
  93. .then(([rows, fields]) => {
  94. if (rows == friendReferCode) {
  95. console.log("Codice uguale!")
  96. return res.send(JSON.stringify({
  97. "status": 500,
  98. "response": "sameCodeAsMine"
  99. }));
  100. }
  101. console.log("Codice non uguale!")
  102. })
  103. }
  104.  
  105. checkIfUserCodeExist()
  106. .catch(next)
  107. });
  108. module.exports = router;
Add Comment
Please, Sign In to add comment