NodeJS Async Await Undefined Response - node.js

I am trying to use async/await to wait for a request to complete and return some information before I proceed with the rest of my code. The function logs the correct response, but the await line says it received an undefined value. This is the function I am calling, which logs the correct response here console.log(loginResponse.idToken);
However, this line let newtoken = await AuthHelper.returnValidToken(token) logs an undefined instead of the response. What mistake am I making here?
returnValidToken: async (token) => {
await AuthHelper.msal
.acquireTokenSilent(loginRequest)
.then((loginResponse) => {
AuthHelper.decodedValidToken(
loginResponse.idToken.rawIdToken,
key,
(jsonToken) => {
if (jsonToken.result === "success") {
// debugger;
console.log(loginResponse.idToken);
return (loginResponse.idToken);
}
}
);
})
.catch((err) => {
console.log(err);
});
},

You should convert the code to use async/await completely
returnValidToken: async (token) => {
try{
const loginResponse = await AuthHelper.msal.acquireTokenSilent(loginRequest);
const jsonToken = await AuthHelper.decodedValidToken(loginResponse.idToken.rawIdToken,key)
if (jsonToken.result === "success") {
console.log(loginResponse.idToken);
return (loginResponse.idToken);
}
}
catch(e){
console.log(e)
return null;
}
},
This is assuming that the AuthHelper.decodedValidToken is also async

As far as I understand this, you should be using either async/await - or Promise.then().
But not both.

It's not that changing the style from a mix of async/await with then didn't work.
The problem is that you're missing a return before AuthHelper.decodedValidToken.
That said, I totally agree that you should choose one style (preferably async/await) and stick to it.

Related

return value of a firebase callable function

I have difficulties returning a value using a callable firebase function, while the same code works fine when it is a httprequest.
So what I am doing, is getting some user data, then getting some other data (a list of vessels) and then only return the vessels the user has edit rights for. I am very sure the accessibleVessels object holds some json data: I changed the function in a functions.https.onRequest firebase function and it went fine.
exports.getFormData = functions.https.onCall( (data, context) => {
const uid = context.auth?.uid.toString;
try {
const user = admin.firestore().
doc("users/"+uid)
.get().then((doc: any) => {
return doc.data();
});
const vessels = admin.firestore().
collection("vessels").get()
.then(mapSnapshot((doc: any) => doc.data()));
const accessibleVessels = vessels.filter((vessel: any) => {
return user.hasEditRights.some((right: any) => {
return vessel.name === right;
});
});
return accessibleVessels;
} catch (error) {
console.log(error);
return {"error": true};
}
});
When I run this I get:
Data cannot be encoded in JSON. [Function (anonymous)]
Looking in the documentation I understand that I do need to return json or a promise. I read other answers about this (returning a promise) but don't see how this would work in my example: in the examples I find not much is done with the data, it's just returned. I want to put the data in variables instead of chaining everything, so I can combine both. How should I do this?
The easiest way to fix this is using async/await:
exports.getFormData = functions.https.onCall(async(data, context) => { // 👈
const uid = context.auth?.uid.toString;
try {
const user = await admin.firestore() // 👈
.doc("users/"+uid)
.get().then((doc: any) => {
return doc.data();
});
const vessels = await admin.firestore() // 👈
.collection("vessels").get()
.then(mapSnapshot((doc: any) => doc.data()));
const accessibleVessels = vessels.filter((vessel: any) => {
return user.hasEditRights.some((right: any) => {
return vessel.name === right;
});
});
return accessibleVessels;
} catch (error) {
console.log(error);
return {"error": true};
}
});
I also recommend reading the MDN documentation on async/await, the Firebase documentation on sync, async, and promises and Doug's awesome series on JavaScript Promises in Cloud Functions.

Using async-await in promises

.then(async (rows) => {
//some code
response = await sendEmail(email);
}
Hello, is it acceptable to make the then method in promises asynchronous if we refer to another interface to send email?
While this would work, IMO it's bad style to mix async/await with promise chaining. Why not just
fooPromise()
.then(rows => {
...
return sendEmail(email);
})
.then(response => {
...
})
or
async function foo() {
const rows = await fooPromise();
...
const response = await sendEmail(email);
...
}
Ie, chose one way you like better and stick with it.
Agree with the answer from #derpirscher. The async/await syntax is mostly just syntactic sugar for promises but it can do wonders for legibility. Also you can try:
async function doSomething() {
let response;
try {
response = await sendEmail(email);
} catch (err) {
console.log(err);
return []
}
return response
You can find a resource about async javascript and promises here.

Why does Async firebase fetching is not working? (NODE JS)

Building a NodeJS REST API.
Trying to send load data from FireBase collection, then sending it to the user (as API response).
Looks like the problem is that it's not waits for the firebase fetch to resolve, but send back a response without the collection data. (tried to use ASYNC-AWAIT but its not working)
exports.getChatMessages = async (req, res, next) => {
const chatId = req.params.chatId
const getChatData = () => {
db
.collection('chats')
.doc(chatId)
.collection('messages')
.orderBy('timeStamp', 'asc')
.onSnapshot((snapshot) => {
snapshot.docs.forEach(msg => {
console.log(msg.data().messageContent)
return {
authorID: msg.data().authorID,
messageContent: msg.data().messageContent,
timeStamp: msg.data().timeStamp,
}
})
})
}
try {
const chatData = await getChatData()
console.log(chatData)
res.status(200).json({
message: 'Chat Has Found',
chatData: chatData
})
} catch (err) {
if (!err.statusCode) {
err.statusCode(500)
}
next(err)
}
}
As you can see, I've used 2 console.logs to realize what the problem, Terminal logs looks like:
[] (from console.logs(chatData))
All messages (from console.log(msg.data().messageContent))
Is there any way to block the code unti the firebase data realy fetched?
If I correctly understand, you want to send back an array of all the documents present in the messages subcollection. The following should do the trick.
exports.getChatMessages = async (req, res, next) => {
const chatId = req.params.chatId;
const collectionRef = db
.collection('chats')
.doc(chatId)
.collection('messages')
.orderBy('timeStamp', 'asc');
try {
const chatsQuerySnapshot = await collectionRef.get();
const chatData = [];
chatsQuerySnapshot.forEach((msg) => {
console.log(msg.data().messageContent);
chatData.push({
authorID: msg.data().authorID,
messageContent: msg.data().messageContent,
timeStamp: msg.data().timeStamp,
});
});
console.log(chatData);
res.status(200).json({
message: 'Chat Has Found',
chatData: chatData,
});
} catch (err) {
if (!err.statusCode) {
err.statusCode(500);
}
next(err);
}
};
The asynchronous get() method returns a QuerySnapshot on which you can call forEach() for enumerating all of the documents in the QuerySnapshot.
You can only await a Promise. Currently, getChatData() does not return a Promise, so awaiting it is pointless. You are trying to await a fixed value, so it resolves immediately and jumps to the next line. console.log(chatData) happens. Then, later, your (snapshot) => callback happens, but too late.
const getChatData = () => new Promise(resolve => { // Return a Promise, so it can be awaited
db.collection('chats')
.doc(chatId)
.collection('messages')
.orderBy('timeStamp', 'asc')
.onSnapshot(resolve) // Equivalent to .onSnapshot((snapshot) => resolve(snapshot))
})
const snapshot = await getChatData();
console.log(snapshot)
// Put your transform logic out of the function that calls the DB. A function should only do one thing if possible : call or transform, not both.
const chatData = snapshot.map(msg => ({
authorID: msg.data().authorID,
messageContent: msg.data().messageContent,
timeStamp: msg.data().timeStamp,
}));
res.status(200).json({
message: 'Chat Has Found',
chatData
})
Right now, getChatData is this (short version):
const getChatData = () => {
db
.collection('chats')
.doc(chatId)
.collection('messages')
.orderBy('timeStamp', 'asc')
.onSnapshot((snapshot) => {}) // some things inside
}
What that means is that the getChatData function calls some db query, and then returns void (nothing). I bet you'd want to return the db call (hopefully it's a Promise), so that your await does some work for you. Something along the lines of:
const getChatData = async () =>
db
.collection('chats')
// ...
Which is the same as const getChatData = async() => { return db... }
Update: Now that I've reviewed the docs once again, I see that you use onSnapshot, which is meant for updates and can fire multiple times. The first call actually makes a request, but then continues to listen on those updates. Since that seems like a regular request-response, and you want it to happen only once - use .get() docs instead of .onSnapshot(). Otherwise those listeners would stay there and cause troubles. .get() returns a Promise, so the sample fix that I've mentioned above would work perfectly and you don't need to change other pieces of the code.

Async - Await issue using Twitter API

I'm currently trying to practice using an API with Twitter API.
I'm using Twit package to connect to twitters API but when I try to do a get request I get
Promise { pending }
I have tried using Async-Await but I'm not sure what I'm doing wrong here.
Here is my code:
const Twit = require('twit');
const twitterAPI = require('../secrets');
//Twit uses OAuth to stablish connection with twitter
let T = new Twit({
consumer_key: twitterAPI.apiKey,
consumer_secret: twitterAPI.apiSecretKey,
access_token: twitterAPI.accessToken,
access_token_secret: twitterAPI.accessTokenSecret
})
const getUsersTweets = async (userName) => {
let params = { screen_name: userName, count: 1 }
const userTweets = await T.get('search/tweets', params, await function (err, data, response) {
if (err) {
return 'There was an Error', err.stack
}
return data
})
return userTweets
}
console.log(getUsersTweets('Rainbow6Game'));
Problem
The biggest assumption that is wrong with the sample code is that T.get is expected to eventually resolve with some data.
const userTweets = await T.get('search/tweets', params, await function (err, data, response) {
if (err) {
return 'There was an Error', err.stack
}
return data // 'data' returned from here is not necessarily going to be received in 'userTweets' variable
})
callback function provided as last argument in T.get function call doesn't have to be preceded by an 'await'.
'data' returned from callback function is not necessarily going to be received in 'userTweets' variable. It totally depends on how T.get is implemented and can not be controlled.
Reason
Thing to be noted here is that async / await works well with Promise returning functions which eventually get resolved with expected data, however, that is not guaranteed here
Relying on the result of asynchronous T.get function call will probably not work because it returns a Promise { pending } object immediately and will get resolved with no data. The best case scenario is that everything with your function will work but getUsersTweets function will return 'undefined'.
Solution
The best solution is to make sure that your getUsersTweets function returns a promise which eventually gets resolved with correct data. Following changes are suggested:
const getUsersTweets = (userName) => {
return new Promise ((resolve, reject) => {
let params = { screen_name: userName, count: 1 }
T.get('search/tweets', params, function (err, data, response) {
if (err) {
reject(err);
}
resolve(data);
})
}
}
The above function is now guaranteed to return expected data and can be used in the following way:
const printTweets = async () => {
const tweets = await getUsersTweet(userName);
console.log(tweets);
}
printTweets();
From what I can see on your code, getUserTweets is an async function, so it will eventually return a promise. I'm assuming you will use this value on another function, so you will need to use it inside an async function and use await, otherwise you will always get a promise.
const logTweets = async (username) => {
try {
const userTweets = await getUsersTweets(username);
// Do something with the tweets
console.log(userTweets);
catch (err) {
// catch any error
console.log(err);
}
}
If logging is all you want and you wrapped it inside a function in which you console.log it, you can call that function directly:
logTweets('someUsername');

Redis hGet method goes to deadlock on second call when called asynchronously

The issue is getMasterData when called never prints 'Passed the worst barrier' :(. God knows what I'm doing wrong. This is what I actually want to achieve.
What am I doing wrong here?
Please let me know. Any help will really be appreciated.
Below is my implementation of node redis implementation:
If I run the same this.getAll() in the loop without any await it works perfectly.
For second time await it never returns back from this.cacheStore.getByKey('organizations') and goes in deadlock forever.
No error and no response either.
Also if I remove data2 await. Still, it works fine.
async setKey(key, data) {
try {
const flatteredData = JSON.stringify(data);
return this.cacheStore.hmset(key, key, flatteredData);
} catch (error) {
return error;
}
}
getByKey(key) { // eslint-disable-line consistent-return
return new Promise((resolve, reject) => {
this.cacheStore.hget(key, key, (err, res) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(res));
}
});
});
}
async getAll() {
const cache = await this.cacheStore.getByKey('organizations');
if (cache) return cache;
const organizations = await this.db
.collection('organizations')
.find({})
.toArray();
await this.cacheStore.setKey('organizations', organizations);
return organizations;
}
async getMasterData(){
const data1 = await this.getAll();
const data2 = await this.getAll();
console.log('Passed the worst barrier');
}
You declare setKey() as an async function but when you call it in getAll() you forgot to await for it.
Either remove the async or call it with await. I am not sure this will resolve your error but try it out!

Resources