【问题标题】:Showing "unexpected token admin" error although admin is declared尽管声明了管理员,但显示“意外的令牌管理员”错误
【发布时间】:2019-08-02 00:48:41
【问题描述】:

我正在尝试执行一些顺序异步操作。但出现错误:

解析错误:意外的令牌管理员

虽然我已经声明了这个变量。这是我的代码

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

【问题讨论】:

    标签: node.js firebase google-cloud-functions


    【解决方案1】:

    你现在可能已经想通了,但问题实际上不是管理员,而是你正在使用 async/await 执行一个函数,而没有声明它是异步的,所以你只需要在函数中放入 async定义如下:

    .then(async (requestDocData) => {
    
          const token = await admin.database().ref("/UserInfo/" + notifiedUserID + "/token").once('value');
          console.log("UserInfo "+token);
          return null;
    
        }
    

    【讨论】:

      【解决方案2】:

      这不是 firebase-admin 的问题,问题是:

      • 您正在使用没有异步的等待
      • 使用函数表达式语法代替箭头回调

      • then() 应该返回一个值或抛出

      • 在 then 之前使用 catch

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-14
        • 1970-01-01
        • 2022-06-14
        • 1970-01-01
        • 2021-08-12
        • 2020-05-09
        • 1970-01-01
        相关资源
        最近更新 更多