【问题标题】:unexcpected token db in cloud function云功能中的意外令牌数据库
【发布时间】:2021-08-11 20:42:12
【问题描述】:

我正在使用 firebase 云功能,我想在 firebase 数据库中添加产品时发送通知。为此,我正在从 firestore 获取设备令牌。但是,当我运行 firebase deploy 时,我收到以下错误消息:

 error  Parsing error: Unexpected token db

这是 index.js 中的代码

/* eslint-disable */ 
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const db = admin.firestore();

exports.sendToDevice = functions.database
  .ref('products/{productId}')
  .onCreate((snapshot, context) => {
   // const product = snapshot.val();
   
    const querySnapshot = await db
      .collection('users')
      .doc('abcdefgh1235678')
      .collection('tokens')
      .get();

    const tokens = querySnapshot.docs.map(snap => snap.id);
    admin.messaging().sendToDevice(tokens,{ 
        notification: {
            title: 'New Product!',
            body: `New product is  ${context.params.productId}`,
            click_action: 'FLUTTER_NOTIFICATION_CLICK'
          }
    });

    return;
});

更新答案后出现以下错误:

error 解析错误:Unexpected token =>

/* eslint-disable */ 
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();  
const db = admin.firestore();

exports.printitemcreated = functions.database.ref('/products/{productId}')
    .onCreate(async(snapshot, context) => {
      const original = snapshot.val();     
      const querySnapshot = await db
            .collection('users')
            .doc('abcdefg123456')
            .collection('tokens')
            .get();
      
          const tokens = querySnapshot.docs.map(snap => snap.id);
          await admin.messaging().sendToDevice(tokens,{ 
              notification: {
                  title: 'New Product!',
                  body: `New product is  ${context.params.productId}`,
                  click_action: 'FLUTTER_NOTIFICATION_CLICK'
                }
          });
      return;
    });

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore google-cloud-functions


    【解决方案1】:

    AFAIK db 不是 Cloud Functions 的保留关键字。

    问题很可能来自您没有initialize an admin app instance。你应该这样做admin.initializeApp();

    此外,您的 Cloud Function 包含其他(关键)错误:您使用 await 而不将处理程序声明为 async 并且您没有等待对 admin.messaging() 的调用。

    以下代码应该可以工作:

    const functions = require("firebase-functions");
    const admin = require('firebase-admin');
    
    admin.initializeApp(); // <- See here
    
    const db = admin.firestore();
    
    exports.sendToDevice = functions.database
      .ref('products/{productId}')
      .onCreate(async (snapshot, context) => {    // <- See async here
       // const product = snapshot.val();
       
        const querySnapshot = await db
          .collection('users')
          .doc('abcdefgh1235678')
          .collection('tokens')
          .get();
    
        const tokens = querySnapshot.docs.map(snap => snap.id);
        await admin.messaging().sendToDevice(tokens,{    // <- See await here
            notification: {
                title: 'New Product!',
                body: `New product is  ${context.params.productId}`,
                click_action: 'FLUTTER_NOTIFICATION_CLICK'
              }
        });
    
        return null;    // <- See null here
    });
    

    【讨论】:

    • 进行建议的更改后,我现在收到此错误:解析错误:意外令牌 =>
    • 我不确定我能否再为您提供帮助......我的答案中的确切代码在我的一个测试 Firebase 项目中正确部署。
    • 查看最后的更改:return null; 而不仅仅是return;
    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多