Showing “unexpected token admin” error although admin is declared

风流意气都作罢 提交于 2020-08-09 09:31:44

问题


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);
    });
  }
}

回答1:


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;

    }



回答2:


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



来源:https://stackoverflow.com/questions/57318668/showing-unexpected-token-admin-error-although-admin-is-declared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!