云功能区域规范错误

时间:2019-09-23 20:35:29

标签: typescript firebase google-cloud-functions

我希望使用Firestore触发器部署以TypeScript编写的Cloud Function。该脚本可以正常工作。但是,我需要指定执行区域,以遵守我的RGPD,我需要在europe-west1中执行功能。我查看了有关该主题的文档,指定在函数中添加“ .region”,但这会生成一个错误,您将发现该错误。获得的错误是“错误TS2339:类型上不存在属性'region' “ DocumentBuilder。我想指出的是,我在本地VSC中没有任何错误,只是我连接到Firebase。

Index.ts

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();

const db = admin.firestore();
const fcm = admin.messaging();

export const sendToDevice = functions.firestore
  .document('orders/{orderId}')
  .region('europe-west1')
  .onCreate(async snapshot => {


    const order = snapshot.data();

    const querySnapshot = await db
      .collection('users')
      .doc(order.seller)
      .collection('tokens')
      .get();

    const tokens = querySnapshot.docs.map(snap => snap.id);

    const payload: admin.messaging.MessagingPayload = {
      notification: {
        title: 'New Order!',
        body: `you sold a ${order.product} for ${order.total}`,
        icon: 'your-icon-url',
        click_action: 'FLUTTER_NOTIFICATION_CLICK'
      }
    };

    return fcm.sendToDevice(tokens, payload);
  });

package.json:

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.1.0"
  },
  "devDependencies": {
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  },
  "private": true
}

感谢您将来的回答。enter image description here

1 个答案:

答案 0 :(得分:0)

根据documentation,区域规范紧随functions构建器声明之后,如下所示:

export const sendToDevice = functions
  .region('europe-west1')
  .firestore
  .document('orders/{orderId}')
  .onCreate(async snapshot => {

请注意,区域规范紧随functions之后。您拥有的内容将在构建器中稍后出现,这是无效的。

请务必使用API documentation来确定在构建函数的每个阶段哪些方法有效。