1

In this code, I am sending a POST request to the server which contains email-id and using that email-id I am retrieving Information from the database. I am testing this from postman "http://127.0.0.1:80/echo" POST request after sending a request first time it shows in response

{
    "source": "webhook-echo-sample"
}

and the second time it gives me proper result

{
    "speech": "Name:xyz Mobile.no:123456789Address:qweCollege:azx Email:cvb@gmail.com",
    "source": "webhook-echo-sample"
}

Why my first request to API is going fail?

"use strict";
const express = require("express");
const restService = express();
const bodyParser = require("body-parser");
const os = require('os');

var mysql = require("mysql");
var final;
var Name = "Name:";
var Mobileno = "Mobile.no:";
var College = "College:";
var Address = "Address:";
var Email = "Email:";

restService.use(
    bodyParser.urlencoded({
        extended: true
    })
);

restService.use(bodyParser.json());
restService.post("/echo", function(req, res) {

    var con = mysql.createConnection({
        host: "abc.com",
        user: "xyz123",
        password: "123456",
        database: "seller1"
    });
    var email = req.body.result.parameters.echoText
    var sql = 'SELECT * FROM seller1 WHERE email_id=' + mysql.escape(email);
    con.query(sql, function(error, results) {
        if (!error) {
            for (var i = 0; i <= results.length - 1; i++) {
                final = Name + results[i].Name + "\n" + Mobileno + results[i].mobile_no + "\n" + Address + results[i].Address + "\n" + College + results[i].College + "\n" + Email + results[i].email_id;
            }
        } else {
            return [];
        }
    });

    return res.json({
        "speech": final,
        "source": "webhook-echo-sample"
    });
});

restService.listen(process.env.PORT || 80, function() {
    console.log("Server up and listening");
});
 
Janith
  • 2,730
  • 16
  • 26
sheel
  • 467
  • 8
  • 23
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Luca Kiebel Sep 20 '18 at 09:54

1 Answers1

2

I think it's because of javascript Asynchronous nature

try sending the response inside !error check as shown below

restService.post("/echo", function(req, res) {

    var con = mysql.createConnection({
        host: "abc.com",
        user: "xyz123",
        password: "123456",
        database: "seller1"
    });
    var email = req.body.result.parameters.echoText
    var sql = 'SELECT * FROM seller1 WHERE email_id=' + mysql.escape(email);
    con.query(sql, function(error, results) {
        if (!error) {
            for (var i = 0; i <= results.length - 1; i++) {
                final = Name + results[i].Name + "\n" + Mobileno + results[i].mobile_no + "\n" + Address + results[i].Address + "\n" + College + results[i].College + "\n" + Email + results[i].email_id;
            }
            return res.json({
              "speech": final,
              "source": "webhook-echo-sample"
            });
        } else {
            return [];
        }
    }); 
});
Atishay Jain
  • 1,425
  • 12
  • 22