Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TypeError: Cannot read property 'query' of undefined
- at checkIfUserCodeExist (/usr/my_server/addReferFriend.js:13:29)
- [...]
- var express = require('express');
- var path = require('path');
- var logger = require('morgan');
- var cookieParser = require('cookie-parser');
- var bodyParser = require('body-parser');
- var mysql= require('mysql2');
- var http = require('http');
- var app = express();
- var addReferFriend = require('./addReferFriend');
- app.set('views', path.join(__dirname, 'views'));
- app.set('view engine', 'ejs');
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(cookieParser());
- app.use(express.static(path.join(__dirname, 'public')));
- app.use('/api/addReferFriend', addReferFriend);
- app.use(async function(req, res, next) {
- try {
- if( req.dbConnection ) {
- // ensure that req.dbConnection was not set already by another middleware
- throw new Error('req.dbConnection was already set')
- }
- let connection = mysql.createConnection({
- host: 'xx',
- user: 'xx',
- password: 'xx',
- database: 'xx'
- });
- res.on("finish", function() {
- // end the connection after the resonponse was send
- req.dbConnection.end()
- });
- // wait for the connection and assign it to the request
- req.dbConnection = await connection.connect();
- next();
- } catch(err) {
- next(err);
- }
- });
- // catch 404 and forward to error handler
- app.use(function(req, res, next) {
- var err = new Error('Not Found');
- err.status = 404;
- next(err);
- });
- module.exports = app;
- var server = http.createServer(app);
- server.listen(3955);
- var express = require('express');
- var router = express.Router();
- /* GET users listing. */
- router.post('/', function(req, res, next) {
- var uid = req.body.uid;
- var friendReferCode = req.body.friendReferCode;
- var sqlCheckIfExist = "SELECT my_refer FROM hub_user WHERE my_refer = '" + friendReferCode + "'";
- var sqlCodeCheckSameAsMine = "SELECT my_refer FROM hub_user WHERE uid = '" + uid + "'";
- function checkIfUserCodeExist() {
- return req.dbConnection.query(sqlCheckIfExist)
- .then(([rows, fields]) => {
- if (rows == 0) {
- console.log("Non esiste!")
- return res.send(JSON.stringify({
- "status": 500,
- "response": "codeNotExist"
- }));
- }
- console.log("Esiste!")
- console.log(rows[0].my_refer);
- return checkIfCodeIsSameAsMine(connection)
- })
- }
- function checkIfCodeIsSameAsMine() {
- return req.dbConnection.query(sqlCodeCheckSameAsMine)
- .then(([rows, fields]) => {
- if (rows == friendReferCode) {
- console.log("Codice uguale!")
- return res.send(JSON.stringify({
- "status": 500,
- "response": "sameCodeAsMine"
- }));
- }
- console.log("Codice non uguale!")
- })
- }
- checkIfUserCodeExist()
- .catch(next)
- });
- module.exports = router;
Add Comment
Please, Sign In to add comment