Async / Await fails in Node JS - node.js

I am having some issues with Async/Await when trying to process my MSSQL Query.
The query works fine because if i do
console.log(sqltest.getNextId("A"))
I get the expected result. But i need to wait for the result from first query to pass on to second one so i tried below. Am i missing here something ? I would like to keep it as clean as possible because i might have the need to execute more then 2 in Sequence.
async function someMethod() {
var newId = await sqltest.getNextId("A")
var nextId = await sqltest.updateId("A",newId)
console.log(newId + ' - ' + nextId)
}
someMethod()
Here is what my getNextId looks like
const getNextId = (counter_id) =>
{const params = [{ name: "p_counter_id", type: sql.VarChar(10), value: counter_id }]
sqlUtil
.storedProcedure(params, "sp_counter_sel")
.then(result =>
{
var newCounter = sequence
(
result.recordset[0].next_id,
result.recordset[0].counter_length,
result.recordset[0].counter_fill
)
console.log(newCounter)
return newCounter
}
)
.catch(err => {
console.log('Error: ' + err.message)
})
}
ok to eliminate all confusion here is my .storedProcedure code
let storedProcedure = async (params, storedProcedureName) => {
const pool = await getOrCreatePool()
let request = await pool.request()
params.forEach((parameter) => {
parameterDirection = parameter.isOutput ? 'output' : 'input';
request = request[parameterDirection](parameter.name, parameter.type, parameter.value)
})
try {
return await request.execute(storedProcedureName)
sql.on('error', err => {
})
} catch(err) {
let message = {
message: {
msg: err.message,
number: err.number
},
}
throw message;
}
}

There is no underhanded error it just returns undefined - undefined for the console.log
This comment is actually is the important bit!
getNextId does not actually return anything, not the value you are looking for, and not a promise that you can await. The reason you are seeing output is that getNextId runs a console.log in the then callback.
But console.log(sqltest.getNextId("A")) is probably spitting out an undefined
Since sqlUtil.storedProcedure seems to return a promise (you can tell because you call then() on it, you should be able to convert this to an async function, and await on that.
const getNextId = async (counter_id) => {
const params = [{
name: "p_counter_id",
type: sql.VarChar(10),
value: counter_id
}]
const result = await sqlUtil.storedProcedure(params, "sp_counter_sel")
var newCounter = sequence(
result.recordset[0].next_id,
result.recordset[0].counter_length,
result.recordset[0].counter_fill
)
return newCounter
}
console.log(await getNextId("A")); // Should be what you expect.
This function now has a return value. Because it is now async and has a return statement that is not nested inside another function. It now returns a Promise that resolves to newCounter, and can be called with await.

Related

Async/await function is returning "undefined" instead of expected String value

I am trying to learn how to work with async functions. I have my code below. There are two functions. The first, apiAddTask, calls the second function SchedulesDAO.GetScheduleByTitle. Both functions are static async functions and 'await' is used when calling the GetScheduleByTitle function.
In the GetScheduleByTitle function, I grab the needed ID from a Mongo Database. The function is correctly grabbing the ID, but is not returning it. Instead it returns undefined.
apiAddTask function:
static async apiAddTask(req, res, next){
try{
let taskFrequency = null
const taskTitle = req.body.title
const taskRepeat = req.body.repeat
if (taskRepeat == true){
taskFrequency = req.body.frequency
}
const taskStart = req.body.startdate
const taskEnd = req.body.enddate
let taskSchedule = await SchedulesDAO.GetScheduleByTitle(req.body.schedule) //CALL TO GETSCHEDULESBYTITLE <--
console.log(taskSchedule) // THIS PRINTS UNDEFINED <------------
const addResponse = await SchedulesDAO.addTask(
taskTitle, taskRepeat, taskFrequency, taskStart, taskEnd, taskSchedule
)
res.json({status:"success"})
}catch(e){
res.status(500).json({error: e.message})
}
}
GetScheduleByTitle Function:
static async GetScheduleByTitle(title) {
let query = {title: title}
let idString = ""
let cursor
try{
cursor = await schedules.find(query)
}catch (e) {
console.error("Unable to issue find command: " + e)
return {schedule: []}
}
cursor.toArray(function(err, results){
console.log(results[0]._id.toString()) //THIS PRINTS THE RIGHT ID STRING <-------------
idString = results[0]._id.toString()
return idString
})
}
I haven't been able to figure out exactly what I am missing. Please let me know if it would be helpful to see any other code.
Most of your code is fine. The issue lies in the fact that you are using a callback within your GetScheduleByTitle function:
async function () {
cursor.toArray(function(err, results){
console.log(results[0]._id.toString()) //THIS PRINTS THE RIGHT ID STRING <-------------
idString = results[0]._id.toString()
return idString
})
// implicit return undefined
}
What is happening is that your cursor.toArray() is being called, as you expect, but it is then continuing execution onto the implicit undefined return. Your function returns undefined before any of the callback logic is even processed.
The way to fix this:
try {
const results = await cursor.toArray()
let idString = results[0]._id.toString()
return idString
} catch (err) {
console.trace(err)
throw new Error(err)
}
Use the asynchronous promise based method.
If one does not exist, you could create a wrapper for a callback only function like so:
const cursorToArray = async (cursor) => {
return new Promise((resolve, reject) => {
cursor.toArray(function(err, results){
if (results)
resolve(results)
else
reject(err)
})
})
}

How to call a custom function synchronously in node js?

I am developing a server project which needs to call some functions synchronously. Currently I am calling it in asynchronous nature. I found some similar questions on StackOverflow and I can't understand how to apply those solutions to my code. Yet I tried using async/await and ended up with an error The 'await' operator can only be used in an 'async' function
Here is my implementation
function findSuitableRoom(_lecturer, _sessionDay, _sessionTime, _sessionDuration, _sessionType){
let assignedRoom = selectRoomByLevel(preferredRooms, lecturer.level, _sessionDuration); <------- Need to be call synchronously
if (!assignedRoom.success){
let rooms = getRooms(_sessionType); <------- Need to be call synchronously
assignedRoom = assignRoom(_lecturer.rooms, _sessionDuration, _lecturer.level);
} else {
arr_RemovedSessions.push(assignedRoom.removed)
}
return assignedRoom;
}
function getRooms(type){
switch (type){
case 'Tutorial' : type = 'Lecture hall'
break;
case 'Practical' : type = 'Lab'
break;
default : type = 'Lecture hall'
}
Rooms.find({type : type},
(err, rooms) => {
if (!err){
console.log('retrieved rooms ' + rooms)
return rooms;
}
})
}
Here I have provided only two methods because full implementation is very long and I feel if I could understand how to apply synchronous way to one method, I can manage the rest of the methods. Can someone please help me?
Well yes await is only available inside an async function so put async infront of findSuitableRoom.
Also you did a classic mistake. You use return inside of a callback function, and expect getRooms to return you some value.
async function findSuitableRoom(
_lecturer,
_sessionDay,
_sessionTime,
_sessionDuration,
_sessionType
) {
let assignedRoom = selectRoomByLevel(
preferredRooms,
lecturer.level,
_sessionDuration
);
if (!assignedRoom.success) {
try {
let rooms = await getRooms(_sessionType);
} catch (err) {
console.log("no rooms found");
}
assignedRoom = assignRoom(
_lecturer.rooms,
_sessionDuration,
_lecturer.level
);
} else {
arr_RemovedSessions.push(assignedRoom.removed);
}
return assignedRoom;
}
Also wrap it in an try / catch
Since .find() returns an promise if you dont pass an callback you can write it like this
function getRooms(type) {
switch (type) {
case "Tutorial":
type = "Lecture hall";
break;
case "Practical":
type = "Lab";
break;
default:
type = "Lecture hall";
}
return Rooms.find({ type });
}
Note here findSuitableRoom is no longer synchronouse. Its async and returns an promise. That means you will need to use the function like this:
findSuitableRoom.then(res => { console.log(res); })
The 'await' operator can only be used in an 'async' function
This means whenever you want to use the await keyword it needs to be inside a function which has an async keyword (returns promise)
read this https://javascript.info/async-await for more info
const add = (a, b) => {
return new Promise((resolve, reject) => {
setTimeout(() => { //to make it asynchronous
if (a < 0 || b < 0) {
return reject("don't need negative");
}
resolve(a + b);
}, 2000);
});
};
const jatin = async () => {
try{
const sum = await add(10, 5);
const sum2 = await add(sum, -100);
const sum3 = await add(sum2, 1000);
return sum3;
} catch (e) {
console.log(e);
}
};
jatin()
Try this
let's understand this
add is a normal function that does some asynchronous action like
waiting for 2 seconds
normally we use await with async function so in order to use it we make as async function jatin and use await with add function call
to make it synchronous, so until first await add call() doesn't
happen it wont execute another await add call().
Example code if you will use in your app.js
router.post("/users/login", async (req, res) => {
try {
const user = await User.findByCredentials(
req.body.email,
req.body.password
);
const token = await user.generateToken();
res.status(200).send({
user,
token,
});
}
catch (error) {
console.log(error);
res.status(400).send();
}
});

Unable to get async/await working with Axios and array.map()

I'm trying to build a function that maps over a given array and then runs an axios call on each item. Once that is done, I want to return the mapped array for use in another function.
Here is the code:
require('dotenv').config();
const axios = require('axios');
const consola = require('consola');
function getLeagues(listItems) {
const today = new Date();
const season = today.getMonth() >= 6 ? today.getFullYear() : today.getFullYear() - 1; // this gets the game ready for the new season every July 1st
// we will end up directly returning listItems.map once the issue is solved
const selectedLeagues = listItems.map(async (item) => {
const countryName = item.country;
const leagueName = item.league;
try {
const response = await axios({
method: 'GET',
url: `https://api-football-v1.p.rapidapi.com/v2/leagues/country/${countryName}/${season}`,
headers: {
'content-type': 'application/octet-stream',
'x-rapidapi-host': 'api-football-v1.p.rapidapi.com',
'x-rapidapi-key': process.env.FOOTY_API_KEY,
},
});
const leagueData = response.data.api.leagues
.filter((league) => league.name === leagueName)
.map((data) => {
return {
leagueId: data.league_id,
name: data.name,
seasonStart: data.season_start,
seasonEnd: data.season_end,
country: data.country,
};
})
.pop(); // we use pop() because filter() and map() return arrays and we don't want an array of 1 object, just that object
consola.ready({
// this displays all of the data as needed
// this also runs after the below consola block
message: `leagueData: ${JSON.stringify(leagueData, null, 2)}`,
badge: true,
});
return leagueData;
} catch (error) {
throw new Error(error);
}
});
consola.ready({
// this displays an array with an empty object, not an array with above leagueData object
// this also runs before the above consola block
message: `selectedLeagues: ${JSON.stringify(selectedLeagues, null, 2)}`,
badge: true,
});
return selectedLeagues;
}
module.exports = getLeagues;
I'm not sure why the selectedLeagues array is being returned before the leagueData object is even ready. I thought async/await held everything. Instead, in my console, I am getting:
selectedLeagues: [
{}
]
leagueData: {
"leagueId": 753,
"name": "Liga 3",
"seasonStart": "2019-07-19",
"seasonEnd": "2020-05-16",
"country": "Germany"
}
What am I doing wrong?
You have to wrap your listItems.map in a promise all function because map on its own isn't compatible with async.
// Now magically you can add async to your map function...
Promise.all(listItems.map(async item => {
// Then put your try catch here so that it only wraps around
// the results of the function you're awaiting...
let response
try {
response = await axios()
} catch (err) {
return err;
}
// Anything else you want to do with the response...
return response
})).then(results => {
// All the resolved promises returned from the map function.
console.log(results)
})
When you use the await keyword inside an async function the rest of the code will just wait for the result of the awaited function, the try catch part is to catch any error you might get that's out of your control which is why you only try catch around the awaited function.
If you wrap too much of your code inside a try catch you won't be able to diagnose and handle the error properly.
You could put a try catch around the whole of your code if you wanted but the problem would be that the whole code would error out whenever you have any kind of small problem.
You can also do this with a for of loop which might look a bit cleaner...
for await (let item of listItems) {
// try catch await axios etc...
}
You can use async together with a Promise,
const arr = [1, 2, 3];
const asyncRes = await Promise.all(arr.map(async (i) => {
await sleep(10);
return i + 1;
}));
console.log(asyncRes);
// 2,3,4
What's happening in your .map() is async, not what's outside of it. The .map() kicks off but it doesn't block the consola.ready({ at the bottom.

Get back empty nodejs + firebase

Guys I need some help with a get return with node and firebase.
I get an empty array.
but in console.log inside foreach prints correct
[{cnpj: '03052164798-000179', Address: 'Av Duke of Caxias 99999', Name: 'Testing', Tel: '999999999'}]
getEmpresas() {
let empresas = []
firebase.firestore().collection('empresa').get().then(snapshot => {
snapshot.docs.forEach(empresa => {
empresas.push(empresa.data())
console.log(empresas)
});
})
return empresas
I think this is a question about async. You're not getting the data you expect in the return because that fires before the async request is made. You'd need something like:
const getEmpresas = async () => {
let empresas = []
const snapshot = await firebase
.firestore()
.collection('empresa')
.get();
snapshot.docs.forEach((empresa) => {
empresas.push(empresa.data());
});
return empresas;
};
As imjared said in his answer, the function returns before .get() gets executed because it is an asynchronous function
You will need to return the chain of firebase functions which will return a Promise
getEmpresas() {
return firebase.firestore().collection('empresa').get().then(snapshot => (
snapshot.docs.map(({ data }) => data())
))
}
Then to access the returned Promise use .then() on the Promise or await inside an async function
getEmpresas.then(empresas => console.log(empresas))
Or inside an async function
const main = async () => {
const empresas = await getEmpresas()
console.log(empresas)
}
main()

NodeJS execute the next process after then() [duplicate]

This question already has answers here:
Using async/await with a forEach loop
(33 answers)
Closed 3 years ago.
I'm trying to do upsert inside forEach becase the request is an array of objects using Sequelize.upsert method. I do this:
async createInfo (req, res){
newsData = req.body.news,
newsList = [];
newsData.forEach(async values => {
var news = {};
news.inserted_id = values.inserted_id;
if(news.inserted_id == null){
do {
news.inserted_id = crypto.getRandom(5);
var check = await InstitutionNews.count({where: {inserted_id: news.inserted_id}});
} while (check > 0);
}
News.upsert({
institution_id: institution_id,
inserted_id: news.inserted_id,
news_date: values.news_date,
news_title: values.news_title,
description: values.news_description,
created_by: created_by
}).then(ResNews => {
news.news_date = values.news_date;
news.news_title = values.news_title;
news.description = values.news_description;
newsList.push(news);
})
})
console.log("TEST")
}
but the process stop at the then(). It didn't execute the next code like the console.log.
Is there any way to execute next line code after the then(). I need then() because I wanna push the news object into newsList array. Because I need newsList as the if else conditional to do the next process.
Thanks.
Since it sounds like you need to wait for forEach to complete before you do another step, so I'd suggest using something like Promise.all:
async function createInfo(req, res) {
const newsData = req.body.news
const newsList = []
try {
await Promise.all(
newsData.map(
async (values) => {
const news = {}
news.inserted_id = values.inserted_id
// ...
News.upsert({...})
.then(ResNews => {
// ...
newsList.push(news)
})
}
)
)
console.log('newsList', newList)
// do other work
} catch (error) {
// handle errors appropriately
}
}
This way, you're creating an array of promises and waiting for all of them to resolve/finish.
Your current approach with forEach won't work in this case since it won't wait for each asynchronous call to finish before executing the "next" step. Since Promise.all returns a single Promise that you can then "wait" for to resolve before continuing with your next step.
Here's a simple example that somewhat does what you're trying to do:
async function createInfo() {
const newsData = [1, 2, 3, 4, 5]
const newsList = []
await Promise.all(
newsData.map(
async (values) => {
const temp = await Promise.resolve('temp')
console.log('first async call inside values of', values)
Promise.resolve('resolved')
.then((result) => {
newsList.push(`resolved with ${values}`)
})
}
)
)
console.log('newsList after')
console.log(newsList)
}
createInfo()
EDIT
Here's an alternative solution as rightly pointed by #Bergi in the comments:
async function createInfo(req, res) {
const newsData = req.body.news
try {
const newList = await Promise.all(
newsData.map(
async (values) => {
const news = {}
news.inserted_id = values.inserted_id
// ...
// since it doesn't look like you're using any
// data that you'd get back from the `News.upsert` call, wait for
// it to finish and just simply return your `news` object
await News.upsert({...})
news.news_date = values.news_date
news.news_title = values.news_title
news.description = values.news_description
return news
}
)
)
console.log('newsList', newList)
// do other work
} catch (error) {
// handle errors appropriately
}
}
use await instead of then like
await News.upsert({
institution_id: institution_id,
inserted_id: news.inserted_id,
news_date: values.news_date,
news_title: values.news_title,
description: values.news_description,
created_by: created_by
});
news.news_date = values.news_date;
news.news_title = values.news_title;
news.description = values.news_description;
newsList.push(news);

Resources