Firebase 云函数部署错误

时间:2021-02-02 17:10:51

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

在使用 firebase 函数部署我的代码时,出现如下错误。我在两个函数中都收到错误。我认为该错误与 crypto-ts 库有关。如果你能帮忙就太好了。我的应用是一个端到端的加密消息应用。

我的打字稿代码:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as crypto from 'crypto-ts';

admin.initializeApp(functions.config());
const db = admin.firestore();

exports.checkLostMessage = functions.firestore.document("conversations/{conversationId}/messages/{messageId}").onCreate(async (snapshot, context) => {
  const data = snapshot.data();
  const messageId = snapshot.id;
  const { conversationId } = context.params;
  const messageData = data['data'];
  const jsonString = crypto.AES.decrypt(messageData, messageId).toString(crypto.enc.Utf8);
  const messageMap = JSON.parse(jsonString);

  if(messageMap['type'] === "lostMessage"){
    setTimeout(async function(){
     await db.collection("conversations").doc(conversationId).collection("messages").doc(messageId).delete();
    } , 15000);
  }
})


exports.sendNotifications = functions.firestore
  .document('conversations/{conversationId}/messages/{messageId}')
  .onCreate(async (snapshot, context) => {
    
    const { message, senderId} = snapshot.data();
    const messageId = snapshot.id;
    const { conversationId } = context.params;
    
    const conversation = await db
      .collection('conversations')
      .doc(conversationId)
      .get();

    
    
    const members : string[] = conversation.get('members');
    
    members
      .filter((member) => member !== senderId)
      .map(async (member) => {
        const profile = await db.collection('users').doc(member).get();
        const token = profile.get('token');

        const jsonString = crypto.AES.decrypt(message, messageId).toString(crypto.enc.Utf8);
        const data = JSON.parse(jsonString);
        
        
        if (!token) {
          return;
        }

        await admin.messaging().sendToDevice(token, {
          data: {
            conversationId,
            userId: member,
            senderId,
          },
          notification: {      
            title: 'Sohbet - Yeni bir mesajınız var.',
            body: data.message,
            clickAction: 'FLUTTER_NOTIFICATION_CLICK',
          },
        });
      });
  });

我的日志:

!  functions: package.json indicates an outdated version of firebase-functions.
Please upgrade using npm install --save firebase-functions@latest in your functions directory.

=== Deploying to 'sohbetapp-1339e'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint D:\yedekler\sohbetapp\functions
> eslint "src/**/*"

Running command: npm --prefix "$RESOURCE_DIR" run build

> functions@ build D:\yedekler\sohbetapp\functions
> tsc

+  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
+  functions: required API cloudbuild.googleapis.com is enabled
+  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (45.98 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: updating Node.js 12 function checkLostMessage(us-central1)...
i  functions: updating Node.js 12 function sendNotifications(us-central1)...
!  functions[sendNotifications(us-central1)]: Deployment error.
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.      
!  functions[checkLostMessage(us-central1)]: Deployment error.
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.      


Functions deploy had errors with the following functions:
        checkLostMessage
        sendNotifications


To try redeploying those functions, run:
    firebase deploy --only "functions:checkLostMessage,functions:sendNotifications"


To continue deploying other features (such as database), run:
    firebase deploy --except functions

我的 package.json 文件内容:

{
  "name": "functions",
  "scripts": {
    "lint": "eslint \"src/**/*\"",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "12"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "^2.22.0",
    "typescript": "^3.8.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

我的函数文件夹:

enter image description here

我认为错误的根源在于crypto-ts插件。我需要帮助。

1 个答案:

答案 0 :(得分:1)

看到您的 package.json 文件,您似乎还没有安装 crypto-ts 软件包。我们应该在 dependencies 下看到它。

在您的 npm install --save crypto-ts 目录中执行 functions


此外,请注意日志第一行中所指示的内容。 functions: package.json indicates an outdated version of firebase-functions. Please upgrade using npm install --save firebase-functions@latest in your functions directory