簡體   English   中英

無法部署 firestore 雲功能

[英]Can't deploy firestore cloud functions

我正在嘗試將 Elastic Search 集成到我的 Flutter 應用程序中。 我正在嘗試部署一些功能,但似乎沒有任何效果。

這是我的 index.js 代碼:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { Client } = require('@elastic/elasticsearch')

admin.initializeApp(functions.config().firebase);

const env = functions.config();
const auth = {
  username: env.elasticsearch.username,
  password: env.elasticsearch.password,
};

const client = new Client({
    node: env.elasticsearch.url,
    auth: auth
})

exports.createPost = functions.firestore
    .document('bookings/{BookingId}')
    .onCreate( async (snap, context) => {
        await client.index({
            index: 'bookings',
            type: '_doc',
            id: snap.id,
            body: snap.data()
        })
    });

exports.updatePost = functions.firestore
    .document('bookings/{BookingId}')
    .onUpdate( async (snap, context) => {
        await client.update({
            index: 'bookings',
            type: '_doc',
            id: context.params.BookingId,
            body: snap.after.data()
        })
    });

exports.deletePost = functions.firestore
    .document('bookings/{BookingId}')
    .onDelete( snap => {
        client.delete({
            index: 'bookings',
            type: '_doc',
            id: snap.id,
        })
    });

這是 packages.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
}

我使用 npm 安裝了 elasticsearch:

npm install @elastic/elasticsearch

每次部署時,我都會得到以下 output:

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

> lint
> eslint

+  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 cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing codebase default for deployment
i  functions: preparing functions directory for uploading...
i  functions: packaged C:\Users\Danis\Desktop\space-shuttle-functions\functions (68.46 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: updating Node.js 16 function createPost(us-central1)...
i  functions: updating Node.js 16 function updatePost(us-central1)...
i  functions: updating Node.js 16 function deletePost(us-central1)...
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.
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.
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:
        createPost(us-central1)
        deletePost(us-central1)
        updatePost(us-central1)
i  functions: cleaning up build files...
Error: There was an error deploying functions:
- Error Failed to update function createPost in region us-central1
- Error Failed to update function deletePost in region us-central1
- Error Failed to update function updatePost in region us-central1

functions:log向我顯示以下內容:

PS C:\Users\Danis\Desktop\space-shuttle-functions> firebase functions:log
2022-05-25T02:35:40.303705Z ? deletePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
2022-05-25T02:35:40.303713Z ? deletePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/main.js
2022-05-25T02:35:40.303723Z ? deletePost:     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-05-25T02:35:40.303730Z ? deletePost:     at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-05-25T02:35:40.303737Z ? deletePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:40.303744Z ? deletePost:     at require (node:internal/modules/cjs/helpers:102:18)
2022-05-25T02:35:40.303751Z ? deletePost:     at Object.<anonymous> (/workspace/index.js:3:20)
2022-05-25T02:35:40.303757Z ? deletePost:     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-05-25T02:35:40.303764Z ? deletePost:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-05-25T02:35:40.303771Z ? deletePost:     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-05-25T02:35:40.303777Z ? deletePost:     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-25T02:35:40.303784Z ? deletePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:40.304148Z ? deletePost: Could not load the function, shutting down.
2022-05-25T02:35:40.720348431Z E deletePost: Function cannot be initialized. Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging

2022-05-25T02:35:40.921043Z E deletePost: {"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"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."},"authenticationInfo":{"principalEmail":"danishajaib93@gmail.com"},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/space-shuttle-b0738/locations/us-central1/functions/deletePost"}
2022-05-25T02:35:41.776Z ? updatePost: Provided module can't be loaded.
2022-05-25T02:35:41.776039Z ? updatePost: Did you list all required modules in the package.json dependencies?
2022-05-25T02:35:41.776055Z ? updatePost: Detailed stack trace: Error: Cannot find module '@elastic/elasticsearch'
2022-05-25T02:35:41.776063Z ? updatePost: Require stack:
2022-05-25T02:35:41.776077Z ? updatePost: - /workspace/index.js
2022-05-25T02:35:41.776089Z ? updatePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
2022-05-25T02:35:41.776098Z ? updatePost: - /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/main.js
2022-05-25T02:35:41.776109Z ? updatePost:     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
2022-05-25T02:35:41.776116Z ? updatePost:     at Function.Module._load (node:internal/modules/cjs/loader:778:27)
2022-05-25T02:35:41.776124Z ? updatePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:41.776132Z ? updatePost:     at require (node:internal/modules/cjs/helpers:102:18)
2022-05-25T02:35:41.776139Z ? updatePost:     at Object.<anonymous> (/workspace/index.js:3:20)
2022-05-25T02:35:41.776147Z ? updatePost:     at Module._compile (node:internal/modules/cjs/loader:1101:14)
2022-05-25T02:35:41.776155Z ? updatePost:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
2022-05-25T02:35:41.776162Z ? updatePost:     at Module.load (node:internal/modules/cjs/loader:981:32)
2022-05-25T02:35:41.776170Z ? updatePost:     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
2022-05-25T02:35:41.776178Z ? updatePost:     at Module.require (node:internal/modules/cjs/loader:1005:19)
2022-05-25T02:35:41.776649Z ? updatePost: Could not load the function, shutting down.
2022-05-25T02:35:42.152840030Z E updatePost: Function cannot be initialized. Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging

2022-05-25T02:35:42.370105Z E updatePost: {"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"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."},"authenticationInfo":{"principalEmail":"danishajaib93@gmail.com"},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/space-shuttle-b0738/locations/us-central1/functions/updatePost"}

該錯誤似乎表明找不到 elasticsearch 模塊,我不知道為什么。 PS:我是 Javascript、Firebase Cloud Functions 和 Elastic Search 的超級新手。

你的elastic search package沒有安裝成功。 當您運行npm i @elastic/elasticsearch命令時,請確保您位於“ functions ”目錄中。 如果問題仍然存在,您還可以從 npm 獲取最新版本的 package 並將其手動添加到您的 package.json。 您可以將其添加到您的依賴項中:

"@elastic/elasticsearch": "^8.2.0"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM