Reputation: 11
I need some help here:
When I use debugger, everything works, but when debugger is not used, it not working.
When getAjax
function is not called to get data from local server, then it the program is working and it can send mail
function sendMessage(headers_obj, message, callback) {
var pGmail = $('#compose-to').val();
getAjax(pGmail).then(result => {
var email = '';
message = encryptText(result, message);
for (var header in headers_obj) {
email += header += ": " + headers_obj[header] + "\r\n";
}
email += "\r\n" + message;
alert(email);
if(email!=null|| email!=''){
var mail = encodeBase64(email);
var sendRequest = gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': mail
}
})
sendRequest.execute(callback)
}
})
}
function getAjax(pGmail) {
return new Promise((resolve) => {
$.ajax({
url: 'http://localhost:7777/' +
'?pmail=' + pGmail + '&method=where', success: function (result) {
return resolve(result);
}
})
});
}
Upvotes: 1
Views: 117
Reputation: 11
var mysql = require('mysql');
var http = require("http");
var url = require("url");
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "1234",
database: 'publickeyserver'
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected!");
});
http.createServer(function (req, res) {
var query = url.parse(req.url, true).query;
switch (query.method) {
case "insert":
var pkey = new Buffer(query.publickey,
'base64').toString('ascii');
insertKey(query.pmail, pkey, res);
break;
case "where":
getKey(query.pmail, res);
break;
default:
console.log("Error");
}
}).listen(7777);
function insertKey(pmail, publickey, res) {
var value = [[pmail, publickey]];
var sql = "INSERT INTO publickey (GmailId, PublicKey) VALUES ?";
con.query(sql, [value], function (err, result) {
if (err) throw err;
console.log("success");
res.write('success');
res.end();
});
}
function getKey(pmail, res) {
console.log(pmail);
var sql = 'SELECT PublicKey FROM publickey WHERE GmailId = ?';
con.query(sql, [pmail], function (err, result) {
if (err) throw err;
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(result[0]['PublicKey']);
console.log(result[0]['PublicKey']);
});
}
Here is my server code, i use nodejs
Upvotes: 0
Reputation: 5466
Trick lies in the time delay.
When I use debugger, everything works, but when debugger is not used, it not working
When you are in Debug mode, the getAjax
call is able to complete its request and the response is arrived. Since while debugging there is a small time delay for us to step over to next line of code.
When getAjax function is not called to get data from local server, then it the program is working and it can send mail
Obviously there is no need to of time delay here, since you are not calling getAjax
function.
Few Solution Approaches
Option 1 : Make it synchronous
Even though the getAjax is an asynchronous call, we can wire up the email sending code in the response of getAjax call, thus making it synchronous,
Option 2 : Introduce a time delay between getAjax
and email sending code.
Please note in this approach, the time delay which might be higher or lower than the time taken by getAjax function and in some cases it may fail, if the getAjax call doesn't respond within in the time delay.
Choose the option which suits best for your scenario.
Hope it helps!
Upvotes: 1