Reputation: 1115
I'm trying to do some sequential asynchronous operation. But getting error:
Parsing error: Unexpected token admin
Although I've declared this variable. Here is my code
const admin = require('firebase-admin')
module.exports = {
notificationCount: async (change, context) => {
countRef.collection("notification").doc(context.params.reqID).get().then((requestDoc) => {
console.log("Request Info " + requestDoc.data().reqUserName)
return requestDoc.data();
}).then((requestDocData) => {
const token = await admin.database().ref("/UserInfo/" + notifiedUserID + "/token").once('value');
console.log("UserInfo "+token);
return null;
}).catch((error) => {
console.log("Loading failed: ", error);
});
}
}
Upvotes: 2
Views: 1481
Reputation: 1261
This is not the issue of the firebase-admin, the issues are:
using function expression syntax instead of arrow-callback
then() should return a value or throw
use catch preceding to then
Upvotes: 1
Reputation: 4854
You have probably figured out by now, but the problem is not actually admin, but rather that you are executing a function with async/await without having stated that it is asynchronous, so you just need to put in async in the function definition like so:
.then(async (requestDocData) => {
const token = await admin.database().ref("/UserInfo/" + notifiedUserID + "/token").once('value');
console.log("UserInfo "+token);
return null;
}
Upvotes: 9