Shahrear Bin Amin
Shahrear Bin Amin

Reputation: 1115

Showing "unexpected token admin" error although admin is declared

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

Answers (2)

Rashid Iqbal
Rashid Iqbal

Reputation: 1261

This is not the issue of the firebase-admin, the issues are:

  • you are using await without async
  • using function expression syntax instead of arrow-callback

  • then() should return a value or throw

  • use catch preceding to then

Upvotes: 1

Jakob
Jakob

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

Related Questions