Async await issue in nodejs - node.js

I am trying to send messages to mobile numbers using message bird API, i have completed the coding part but its not sending the messages, even i can see i have sent messages and being delivered in my message bird profile. I came to know that this issue can be due to not using async and await. Here is my code,
api.get('/sendmessage', function (req, res) {
var id = req.headers.authorization;
if (req.session.id) {
var userid = req.session.user[0].id;
var username = userInfo.find({ id: userid }, function (err, user) {
if (err) { res.send("New Error: " + err); }
else {
if (!_.isEmpty(user)) {
var contact = user[0].contact;
var messagebird = require('messagebird')('ACCESS_KEY_API'); // Api for testing
var params = {
'originator': 'MessageBird',
'recipients': [
contact
],
'body': 'Hello, world!'
};
messagebird.messages.create(params, function (err, response) {
if (err) {
return console.log("Error sent to Console: " + err);
}
else {
console.log(response);
return res.send(response);
}
});
}
else {
res.send("No Results");
}
}
});
}
});

Related

AWS COGNITO: How to implement Post Confirmation Trigger which works only after SignupConfirmation not after ForgotPassword/ResetPassword Confirmation

Someone can please tell me, that how can send this mail after the Post Confirmation of Signup only. This code is sending mail after every confirmation whether it is forget password/reset password or Signup.
var aws = require('aws-sdk');
var ses = new aws.SES();
exports.handler = (event, context, callback) => {
console.log(event);
if (event.request.userAttributes.email) {
sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function(status) {
// Return to Amazon Cognito
callback(null, event);
});
} else {
// Nothing to do, the user's email ID is unknown
callback(null, event);
}
};
function sendEmail(to, body, completedCallback) {
var eParams = {
Destination: {
ToAddresses: [to]
},
Message: {
Body: {
Text: {
Data: body
}
},
Subject: {
Data: "Cognito Identity Provider registration completed"
}
},
// Replace source_email with your SES validated email address
Source: "<source_email>"
};
var email = ses.sendEmail(eParams, function(err, data){
if (err) {
console.log(err);
} else {
console.log("===EMAIL SENT===");
}
completedCallback('Email sent');
});
console.log("EMAIL CODE END");
};
You can check triggerSource attribute of the event object.
Post Confirmation of Signup event will have trigger source is PostConfirmation_ConfirmSignUp
Your handle function will look like this:
exports.handler = (event, context, callback) => {
console.log(event);
if (
event.request.userAttributes.email
&& event.triggerSource === "PostConfirmation_ConfirmSignUp") {
sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function (status) {
// Return to Amazon Cognito
callback(null, event);
});
} else {
// Nothing to do, the user's email ID is unknown
callback(null, event);
}
};
Reference: User Pool Lambda Trigger Sources

Render the results of two separate async.each methods

I am new to nodejs and async. Having trouble understanding how can I wrap the two separate async.each methods to have one res.render...I am trying to display a list of valid account ids, and valid user ids on the front end.
The two separate async.each methods are:
async.each(account_ids, function(accountId, callback) {
console.log('Processing accountId ' + accountId);
callingExternalApi(accountId, callback, function(err, response){
if(err){
console.log("error account");
}
console.log("account response is: ", response);
});
}, function(err) {
if( err ) {
console.log('An account failed to process');
} else {
console.log('All accounts have been processed successfully');
}
});
and
async.each(email_ids, function(emailId, callback) {
console.log('Processing email id ' + emailId);
request({
url: emailIdlookupUrl,
method: 'POST',
json: {
email_address: emailId
}
}, function (err, response, body) {
if (err) {
logger.error(err);
req.flash('error', err.message);
return res.redirect('?');
}
if (response.statusCode !== 200) {
const msg = 'Unable to verify user';
req.flash('error', msg);
return res.redirect('?');
}
console.log("user id is: ", body.user.id);
callback();
});
}, function(err) {
if( err ) {
console.log('An email failed to process');
} else {
console.log('All user emails have been processed successfully');
}
});
Any help is highly appreciated. Please excuse me for any redundant callbacks or error logging. Still learning nodejs.
Thanks!!
The main issue is not that you are invoking both of these async.each calls. The problem is that they will run in parallel, and the fastest one to invoke req.* functions or callback function will return a response to the connection.
Both of these functions return promises if their callback parameters are omitted.
I recommend reading up on both the async library and JS async/await in general:
https://javascript.info/async-await
https://caolan.github.io/async/v3/docs.html#each
https://zellwk.com/blog/async-await-express/
Note that async also accepts native async functions, which many finder cleaner and easier to understand.
Here is what I think you want from the code above, including compiling the results into lists:
var request = require("request-promise");
async function checkAccounts(account_ids) {
const valid_accounts = [];
await async.each(account_ids, async function(accountId) {
console.log("Processing accountId " + accountId);
const extAPIresult = await callingExternalApi(accountId);
console.log("account response is: ", extAPIresult);
});
valid_accounts.push(extAPIresult);
console.log("All accounts have been processed successfully");
return valid_accounts;
}
async function checkEmails(email_ids) {
const valid_emails = [];
await async.each(email_ids, async function(emailId) {
console.log("Processing email id " + emailId);
const reqresult = await request({
url: emailIdlookupUrl,
method: "POST",
json: {
email_address: emailId
}
});
if (reqresult.statusCode !== 200) {
throw new Error("Unable to verify user");
}
valid_emails.push(reqresult.body.user.id);
console.log("user id is: ", reqresult.body.user.id);
});
console.log("All emails have been processed successfully");
return valid_emails;
}
async function doChecks() {
const accounts = checkAccounts(account_ids);
const emails = checkEmails(email_ids);
const responses = await Promises.all([accounts, emails]);
console.log("All checks have been processed successfully");
return responses;
}
function get(req, res) {
doChecks()
.then(responses => {
res.send("All checks have been processed successfully");
res.send(String(responses));
})
.catch(err => {
req.flash("error", err.message);
res.redirect("?");
});
}

node js recursive concept

I am new to Node.js technology and facing some issues in recursive concept.
I have a main.js which contains list of username and a soap method call Soap.js contains soap method which will fetch email id from username.
------------- Main.js ----------------
'use strict'
var emailService = require('./emailService .js').emailService ;
var emailService1 = new emailService ();
var emailList = [];
var psList = ['1062','10465','10664','10681'];
emailService1.helpdeskEmailService(psList, 'abcabc', 'abcabc', function(err,result) {
console.log('in service -------------------------');
if (err) {
console.log("Error while api call :: " +err);
} else {
console.log("response from soap service - " + result);
}
});
console.log('my email list' +result);
------------- SoapService.js ----------------
'use strict'
var c_instancename = '';
var soap = require('soap');
var l_args;
var c_url = "http://airinmsbmcarmt.lntinfotech.com/arsys/WSDL/public/172.21.103.136/zlandt:FetchEmailID";
class emailService {
constructor(p_instanceName) {
c_instancename = p_instanceName;
}
helpdeskEmailService (ps_number,p_username,p_password,p_callback) {
var l_header = {
'authentication': '',
'locale': '',
'timeZone': '',
'AuthenticationInfo': {
'userName': p_username,
'password': p_password
}
}
soap.createClient(c_url, function(err, client) {
//var soapheader = l_header;
client.addSoapHeader(l_header);
var l_args = {LoginID:ps_number};
client.EmailID(l_args, function(err, result) {
if(err) {
console.log('error page');
} else {
console.log('my resultttttttt in soap...');
p_callback(err,result);
}
});
});
}
}
module.exports.emailService = emailService;
In this case, I'm getting late response from soap service.
Can I have sync call for webservice because I am getting NULL values for emailList.
I have a main.js which contains list of username and a soap method call.
Soap.js contains soap method which will fetch email id from username.
You can return a promise from your service and if you're using Node 8.0+ you can also make the call synchronous. If not at least it makes it easier to deal with asynchronous call.
helpdeskEmailService (ps_number,p_username,p_password) {
var l_header = {
'authentication': '',
'locale': '',
'timeZone': '',
'AuthenticationInfo': {
'userName': p_username,
'password': p_password
}
}
return new Promise (function (resolve, reject) {
soap.createClient(c_url, function(err, client) {
//var soapheader = l_header;
client.addSoapHeader(l_header);
var l_args = {LoginID:ps_number};
client.EmailID(l_args, function(err, result) {
if(err) {
console.log('error page');
reject(err);
} else {
console.log('my resultttttttt in soap...');
resolve(result);
}
});
});
}
}
// You can then call like this
var promise = emailService1.helpdeskEmailService(psList, 'abcabc', 'abcabc');
promise.then((result) => {
console.log("response from soap service - " + result);
}).catch ( (err) => {
console.log("Error while api call :: " +err);
});
You can also use await to make this synchronous (depending on the version of Node you're using):
async function waitForresult() {
try
{
const result = await emailService1.helpdeskEmailService(psList, 'abcabc','abcabc');
console.log(result);
} catch (e) {
console.log('Error occurred: ' + e);
}
};
waitForresult();

twitter.stream is not a function in node.js?

I cannot able to monitor the twitter.I followed the procedure to do the sentiment analysis (twitter) in node.js code.It verified my twitter account correctly.but it shows stream is not a function. I enclosed the code .can anyone solve this issue.Thanks in advance...
app.get('/watchTwitter', function (req, res) {
const twitter = new twitterAPI({
consumerKey: "asas",
consumerSecret: "sdcscs"
});
const accessToken = "cdccd";
const accessTokenSecret = "csdcs";
var stream;
var testTweetCount = 0;
var phrase = 'bieber';
twitter.verifyCredentials(accessToken, accessTokenSecret, params, function (error, data, response) {
if (error) {
console.log(error);
} else {
console.log(data["screen_name"]);
stream = twitter.stream('statuses/filter',
{
'track': phrase
}, function (stream) {
res.send("Monitoring Twitter for \'" + phrase + "\'... Logging Twitter traffic.");
stream.on('data', function (data)
{
testTweetCount++;
if (testTweetCount % 50 === 0)
{
console.log("Tweet #" + testTweetCount + ": " + data.text);
}
});
});
}
});
});
app.listen(8086,function()
{
console.log("port is listen on 8086");
});
You are referring Twitter npm package, but you are using node-twitter-api. See the documentations. For statuses, you need to use following method.
twitter.statuses("update", {
status: "Hello world!"
},
accessToken,
accessTokenSecret,
function(error, data, response) {
if (error) {
console.log(error);
} else {
console.log(data);
}
}
);

Unable to run a function synchronously in nodejs and express

I have used wikipedia-js for this project. This is my code for summary.js file.
var wikipedia = require("wikipedia-js");
var something = "initial";
module.exports = {
wikitext: function(topicname) {
console.log("Inside wikitex funciton :" + topicname);
var options = {
query: topicname,
format: "html",
summaryOnly: false,
lang: "en"
};
wikipedia.searchArticle(options, function(err, htmlWikiText) {
console.log("Inside seararticlefunciton :");
if (err) {
console.log("An error occurred[query=%s, error=%s]", topicname, err);
return;
}
console.log("Query successful[query=%s, html-formatted-wiki-text=%s]", topicname, htmlWikiText);
something = htmlWikiText;
});
return something;
},
};
This module I am using in /wiki/:topicname route. The corresponding code in index.js is like this.
router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
console.log(topicname);
var first = summary.wikitext(topicname);
res.send("Hello "+first);
});
The problem is, everytime i visit a wiki/some-topic, the last return statement of summary.js executes before htmlWikiText is populated with content. So I always see hello initial on the browser page. Although after sometime it gets printed on terminal due to console.log statement.
So how should I resolve this issue?
I'm not going to try turning this code into synchronous. I'll just correct it to work as an asynchronous version.
You need to pass in callback to wikitext() and return the value in that callback. Here is the revised code of wikitext() and the route that calls it:
var wikipedia = require("wikipedia-js");
module.exports = {
wikitext: function(topicname, callback) {
console.log("Inside wikitex funciton :" + topicname);
var options = {
query: topicname,
format: "html",
summaryOnly: false,
lang: "en"
};
wikipedia.searchArticle(options, function(err, htmlWikiText) {
console.log("Inside seararticlefunciton :");
if (err) {
console.log("An error occurred[query=%s, error=%s]", topicname, err);
return callback(err);
}
console.log("Query successful[query=%s, html-formatted-wiki-text=%s]", topicname, htmlWikiText);
callback(null, htmlWikiText);
});
}
};
router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
console.log(topicname);
summary.wikitext(topicname, function(err, result) {
if (err) {
return res.send(err);
}
if (!result) {
return res.send('No article found');
}
res.send("Hello "+result);
});
});

Resources