Guest User

Untitled

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