The function gets resolved on its own and no data is returned from the function but null.
Can anyone solve this for me please?
print is happening like below :
[ RowDataPacket { payment_id: 'PAYID-MDRPLBY9LS10853614735924' } ]
data not 000000
data not 1111111
promise2
Get Payment Response
Payment Object
but it should be :
[ RowDataPacket { payment_id: 'PAYID-MDRPLBY9LS10853614735924' } ]
data not 000000
data not 1111111
Get Payment Response
Payment Object
promise2
async function func(req, res) {
return new Promise((resolve, reject) => {
let email_id = req.body.email_id;
let arr = [];
let ctr = 0;
sql_get_orderid =
"Select payment_id from paypal.execute_order where email_id=?";
db.query(sql_get_orderid, [email_id]).then(function (data) {
console.log(data);
if (data.length != 0) {
console.log("data not 000000");
data.forEach((x) => {
console.log("data not 1111111");
var paymentId = x.payment_id;
paypal.payment.get(paymentId, function (error, payment) {
if (error) {
console.log(error);
reject({ auth: true, message: "Error on the Server" });
} else {
console.log("Get Payment Response");
console.log(JSON.stringify(payment));
arr[ctr] = payment;
ctr = ctr + 1;
// resolve(payment);
// if(ctr==data.length)
}
});
});
resolve(arr);
}
});
});
}
router.post(
"/get_all_orders",
VerifyToken.Token_Verification,
async (req, res) => {
let arr = await func(req, res);
console.log("promise2");
res.send({ data: arr });
}
);
The reason why your logs are not in the order you expect it to be is that forEach in javascript expects a synchronous callback function.
It does not wait for promises or callbacks inside its callback to return.
Therfore in your code when func is invoked and once db.query is performed and if data.length is valid data not 000000 is logged.
Then iteration begins with forEach. Here for Each element in iteration data not 1111111 will be logged and an asynchronous call is made through paypal.paymeny.get method.
As mentioned above, forEach callbacks does not wait for it to get resolved and next iteration is continued and so on.
Once iteraions are done, resolve is invoked. By the time when resolve is invoked paypal.paymeny.get has not yet returned and thus empty array will be passed.
Then when the control is passed to the route handler promise2 is logged.
Get Payment Response is logged later once paypal.payment.get returns.
To avoid this, the execution sequence can be controlled with the help of promises. Please try with the below code.
async function func(req, res) {
try {
const email_id = req.body.email_id;
const sql_get_orderid =
"Select payment_id from paypal.execute_order where email_id=?";
const data = await db.query(sql_get_orderid, [email_id]);
if (!data.length)
return;
console.log('data not 000000');
const getPaymentPromises = data.map(x => new Promise((resolve, reject) => {
console.log("data not 1111111");
paypal.payment.get(x.payment_id, (error, payment) => {
if (error)
return reject(error);
console.log("Get Payment Response");
console.log(JSON.stringify(payment));
resolve(payment);
});
}));
return await Promise.all(getPaymentPromises);
} catch (error) {
return { auth: true, message: 'Error on the Server' };
}
}
Related
I am trying to write an async lambda function which is calling a function for sign up a user in cognito.
my problem is that in my lambda function, it is not waiting for the result and finish the execution. would you mind check what is my issue? I am new to rxjs. please help me.
mylambda function
exports.handler = async (event, context) => {
//poolData and params will fetch from event
let source = await signup(poolData, params);
console.log(source);
});
my signup function
function signup(poolData, body) {
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const { username, password, attributes } = body;
const attributesList = [];
if (Array.isArray(attributes)) {
attributesList.push(
...attributes.map(item => new AmazonCognitoIdentity.CognitoUserAttribute(item))
);
}
let source = Observable.create(observer => {
let output = (err, res) => {
if (err)
{
observer.error(err);
}
else
{
const cognitoUser = res.user;
const data = {
username: cognitoUser.getUsername(),
};
observer.next(data);
}
observer.complete();
}
userPool.signUp(username, password, attributesList, null, output);
});
let respond;
let subscriber = {
next(value) {
console.log('Subscriber - next: ', value);
respond = {
'statusCode': 200,
'body': JSON.stringify({
"username": value.username,
})
}
}, error(err) {
console.log('Subscriber - err: ', err);
respond = err;
},
complete() {
console.log('Subscriber - complete');
return response;
}
};
source.subscribe(subscriber);
}
module.exports = signup;
This behavior is totally normal.
So first thing first, an observable is not a promise which means you are not able to await a response with the await keyword, also I don't see anything to be returned from the signup function, which will probably lead to undefined to be logged anyways.
So how to fix that, one way to fix this issue is to use toPromise() which will turn your observable into a promise which then can be awaited wherever needed.
The other way (which is the rxjs way) will be to return from the signup function the observable and inside your handler function to subscribe for the response.
let subscriber = {
next(value) {
console.log('Subscriber - next: ', value);
respond = {
'statusCode': 200,
'body': JSON.stringify({
"username": value.username,
})
}
}, error(err) {
console.log('Subscriber - err: ', err);
respond = err;
},
complete() {
console.log('Subscriber - complete');
return response;
}
};
exports.handler = (event, context) => {
//poolData and params will fetch from event
signup(poolData, params).subscribe(subscriber);
})
Here I am trying to retrieve objects and push them into the array. For some reason there is only one record being pushed into the file when it should contain more objects. Can you help me out with this or let me know where I am going wrong? Here is my code:
exports.createjson = (req, res, next) => {
try {
var myPromise = () => {
// ...
};
var callMyPromise = async () => {
const responsearray = [];
var result = await myPromise();
return new Promise((resolve, reject) => {
result.forEach(element => {
NewsModel.findOne({ _id: element.newsId }).exec(
async (err, result) => {
if (err) {
throw err;
}
reportsModel
.findOne({
$and: [
{ userId: req.query.userId },
{ newsId: element.newsId }
]
})
.exec((err, newsResult) => {
if (err) {
throw err;
}
// console.log(newsResult);
var response = {
newsId: element.newsId,
title: result.title,
collection: result.group,
belivibalityIndex: element.belivibalityIndex,
priorknowledge: element.priorknowledge,
readingTime: element.readingTime,
userId: element.userId,
comment: element.comment,
report: newsResult !== null ? newsResult.feedback : null
};
// #all object pushed and displayed in console
responsearray.push(response);
console.log(response);
console.log(responsearray.length);
// let data = JSON.stringify(responsearray);
// #here is the issue // fs.writeFileSync("abc.json", data, null, null, flag = 'a');
return responsearray;
});
}
);
});
});
};
callMyPromise().then(function(responsearray) {
res.json(responsearray);
});
} catch (error) {
next(error);
}
};
You're not quite using Promises properly. For example, you create a Promise object but never call the resolve/reject functions. In the forEach loop you are calling functions that use callbacks and when that work is done you can resolve the promise you're wrapping it in.
Also you're calling res.json and writing the file (though it's commented out) while you're in the forEach loop. That means res.json will get called multiple times, which is not allowed. You can only have one response from an http request.
I restructured the code so that it collects each promise in an array of Promises then waits for all of them to resolve. Only after all of the work is done, we can write the file and call res.json to complete the http request.
exports.createjson = async (req, res, next) => {
const responsearray = [];
var elements = await myPromise();
var promises = []; // collect a bunch of promises to wait on
elements.forEach(element => {
// one promise per element that resolves when response is on the array
var promise = new Promise(function(resolve, reject) {
NewsModel.findOne({ _id: element.newsId }).exec((err, result) => {
if (err) { return reject(err); }
reportsModel
.findOne({
$and: [{ userId: req.query.userId }, { newsId: element.newsId }]
})
.exec((err, newsResult) => {
if (err) { return reject(err); }
var response = { /* response body */ };
responsearray.push(response);
console.log(response);
console.log(responsearray.length);
// complete the promise now that the response is on the array
return resolve();
});
});
});
// collect each promise in an array so we can wait for them all
promises.push(promise);
});
// wait for all the work to complete
await Promise.all(promises).catch(err => next(err));
// write the responsearray to a file as json
let data = JSON.stringify(responsearray);
fs.writeFileSync("abc.json", data);
return res.json(responsearray);
};
I also removed the try/catch block since the Promise allows you to use .catch in a cleaner way. It simplifies the nesting which makes it easier to read.
The key takeaway here is the general structure:
// get your array to work with
var array = await someFunction()
var manyPromises = []
var manyResults = []
// for each thing in the array create a promise
array.forEach( thing => {
manyPromises.push( new Promise((resolve,reject) => {
doSomething(thing, (err, result) => {
if (err) return reject(err);
// store the results in the array and resolve the promise
manyResults.push(result)
return resolve();
});
});
});
// wait for all promises in manyPromises to complete
await Promise.all(manyPromises).catch(err => return next(err));
// now the many promises are done and manyResponses are ready
saveResponsesToFile(JSON.stringify(manyResponses))
return res.json(manyReponses)
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("?");
});
}
In the following NodeJS API, a PostgreSQL database is inserted with multiple rows and then a response body needs to be sent. Since there are multiple scenarios where there could be an error, I need to set-up response body with a message code that helps identify a possible cause. Therefore, I am using a resp object that will hold a code key for a message code. This object will be sent in a HTTP response where the status is either 200 (success) or 500 (error).
However, I keep getting an empty response body. Is the variable scope ok?
router.post('/', function (req, res, next) {
rdbmsPool.connect((err, client, release) => {
if (err) {
var resp = {
'Request-Id' : req.get('X-Request-Id'),
'code' : 'code'
}
res.status(500)
res.send(resp)
} else {
var resp = {}
for (k in someArray) {
client.query(queryString, colValues, (err, result) => {
if (err) {
resp = {
'Request-Id' : req.get('X-Request-Id'),
'code' : 'code'
}
res.status(500)
} else {
resp = {
'Request-Id' : req.get('X-Request-Id'),
'code' : 'code'
}
res.status(200)
}
})
}
for (k in someOtherArray) {
client.query(queryString, colValues, (err, result) => {
if (err) {
resp = {
'Request-Id' : req.get('X-Request-Id'),
'code' : 'code'
}
res.status(500)
} else {
resp = {
'Request-Id' : req.get('X-Request-Id'),
'code' : 'code'
}
res.status(200)
}
})
}
release()
res.send(resp)
}
})
});
Try something like this
let array1 = [];
for(k in someArray){
array1.push(client.query(queryString, colValues));
}
let array2 = [];
for (k in someOtherArray) {
array2.push(client.query(queryString, colValues));
}
try{
const results1 = await Promise.all(array1);
const results2 = await Promise.all(array2);
// if it comes here queries are successful
}
catch(error){
//send 500 here. any query have failed
console.log(error);
}
What happens is your response is sent before queries are completed. Your query execution is asynchronous and res.send(resp) does not wait until queries are executed.
The variable resp is set inside callbacks therefore it is empty.
You can try using async-await for synchronized execution with promise wrapper for parrelel execution of queries that are run inside loop. Note async function.
This is how it can be done with node mysql2/promise. Asynchronous means that the callback function you provide in query() will be called after query is executed. and the code after query() will run not waiting until query function is completed.
router.get('/getDayTypes',async function (req, res, next) {
const API_NAME='getDayTypes get, ';
try{
const conn = await pool.getConnection();
// Do something with the connection
var promise1 = conn.execute(`
SELECT
ID_DAY_TYPES,
DAY_NAME
FROM
hrm_calendar_day_types
WHERE
IS_ACTIVE = '1'`);
const values = await Promise.all([promise1]);
conn.release();
return res.status(200).send({success:true, data:values[0][0]});
}
catch(err){
logger.error(API_NAME + 'error :' + err);
return res.status(500).send({success:false, message:'Query Error'});
}
});
I have an array of items that I need to post to my server. I've tried the following but i never iterates.
var i = 0;
while (i < numOfItems) {
var item = items[i];
var a;
for(var ik in item){
console.log(item[ik]);
a = item[ik]; // Gets the key
break;
}
var formData = {
ID : ID,
UID : UID,
item : a
}
request.post({url:'http://example.com/a', formData: formData}, function(err, httpResponse, body){
if (err) {
return console.error('Post failed:', err);
}
console.log('Post successful! Server responded with:', body);
i++;
});
}
Your code won't work because request.post is asynchronous. If your objective is to make a call for each element in the array, a working and a more elegant solution would be to use Promises.all().
Here's your code modified with Promises —
function postRequest(url, formData) {
return new Promise((resolve, reject) => {
request.post({ url, formData }, function (err, httpResponse, body) {
if (!error) {
resolve({ message: 'Post successful!', response: body });
} else {
reject(err);
}
});
})
}
// Map your data array to an array of Promises
let promises = yourArray.map(element => {
let formData = {
ID: ID,
UID: UID,
item: element
}
return postRequest({ url: 'http://example.com/a', formData: formData })
});
// Wait for all Promises to complete
Promise.all(promises)
.then(results => {
// Handle results
})
.catch(e => {
// Handle error
});
A few things to note -
I'm reusing the fields ID and UID as-is, as it isn't clear where they come from in your code.
Replace yourArray with the array containing your data items.