【发布时间】: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