为什么无法将Flutter JavaScript部署到Firebase云函数?

huangapple go评论68阅读模式
英文:

Why can't I deploy Flutter JavaScript to firebase cloud function

问题

我正在在一个 Flutter 项目中实现 push-notifications。我希望 Firebase 函数在数据库中特定 collection 发生变化时触发通知。

使用 JavaScript 编写该函数。
Node 版本:16

复现步骤:

步骤 1:firebase init functions

步骤 2:? 请选择一个选项:使用现有项目

步骤 3:? 为此目录选择一个默认的 Firebase 项目:my-project-1 (my-project-1)

步骤 4:? 您想要使用哪种语言编写云函数?JavaScript

步骤 5:? 您想要使用 ESLint 来捕获可能的错误并强制执行样式吗?是的

步骤 6:index.js 中的 JavaScript 代码

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

// fcm notification
exports.sendNotification = functions.firestore
  .document("rooms/{roomId}/")
  .onWrite(async (change, context) => {

    const payload = {
      "notification":{
        "title": "Firebase messaging",
        "body": "A message",
      },
    };
    const options = {};
    return admin.messaging().sendToDevice('TOKEN-ID', payload, options);
  });

步骤 7:npm run deploy

在部署到 Firebase 时,返回以下错误:

9:34  错误  解析错误:意外的标记 =>

✖ 1 问题 (1 个错误,0 个警告)

错误: 函数预部署错误:命令以非零退出代码 1 终止
noname@inoname functions % npm run deploy

这是 package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}
英文:

I am implementing push-notifications in a Flutter project. I want Firebase functions to trigger the notification whenever there is a change in a certain
collection in the database.

Using JavaScript to write the function.
Node version: 16

To reproduce:

Step 1: firebase init functions

Step 2: ? Please select an option: Use an existing project

Step 3: ? Select a default Firebase project for this
directory: my-project-1 (my-project-1)

Step 4: ? What language would you like to use to write Cloud Functions? JavaScript

Step 5: ? Do you want to use ESLint to catch probable bugs and enforce style? Yes

Step 6: JavaScript code for index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

// fcm notification
exports.sendNotification = functions.firestore
  .document("rooms/{roomId}/")
  .onWrite(async(change, context) => {

    const payload = {
      "notification":{
        "title": "Firebase messaging",
        "body": "A message",
      },
    };
    const options = {};
    return admin.messaging().sendToDevice('TOKEN-ID', payload, options);
  });

Step 7: npm run deploy

When deploying to firebase, this is the error returned:

9:34  error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)

Error: functions predeploy error: Command terminated with non-zero exit code 1
noname@inoname functions % npm run deploy


This is the package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

答案1

得分: 0

ChatGPT提供了以下代码,它在某种程度上能够正常工作。我无法察觉到区别,但现在它可以正常工作:

*编辑:我发现是async属性导致了问题。通过移除asyncawait,它编译并部署成功。

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

exports.chatMessageNotification = functions.firestore
    .document("rooms/{roomId}")
    .onUpdate((change, context) => {
      const newData = change.after.data();
      const previousData = change.before.data();

      if (newData.updatedAt !== previousData.updatedAt) {
         // 在这里执行您的通知发送逻辑
         // 使用newData和previousData访问更新后和之前的文档数据
         // 将通知发送到所需的设备令牌
      }
    });
英文:

Asked ChatGPT, which provided me this code that somehow works. I can't spot the difference, but now it works:

*Edit: Found out it was the async property causing the problem. By removing async and await it compiled and deployed

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

exports.chatMessageNotification = functions.firestore
    .document("rooms/{roomId}")
    .onUpdate((change, context) => {
      const newData = change.after.data();
      const previousData = change.before.data();

      if (newData.updatedAt !== previousData.updatedAt) {
         // Perform your notification sending logic here
         // Use newData and previousData to access the updated and previous document data
         // Send the notification to the desired device token
      }
    });

huangapple
  • 本文由 发表于 2023年6月29日 03:46:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定