這是我第一次嘗試部署一個函式 firebase 函式。我撰寫了一個 API,我想創建一個 firebase 函式并使用它。
在我的專案中,一切都在 localhost 上作業,甚至在我做的時候也作業firebase serve --only functions, hosting。
因為我只是在使用hosting并且functions我沒有做initializeApp(firebaseConfig)firebase config 的事情(不確定這是否需要)。
我functions/index.js的是:
//functions/index.js
const functions = require("firebase-functions");
const express = require('express');
const bodyParser = require('body-parser');
var connection = require('../utils/dbconfig'); //outside functions folder
const app = express();
app.use(bodyParser.json());
// API START HERE
app.get('/getEmployees', (req, res) => {
// Here I connect to db which has it's configurations in dbConfig.js
res.send(result);
});
// API END HERE
exports.app = functions.https.onRequest(app);
我已經從index.js我在主專案檔案夾(外部函式)中手動粘貼了這段代碼,在function我有另一個的檔案夾中index.js,以及package.json自動生成的檔案,我已經添加了依賴項,就像我package.js在外部functions檔案夾。然后在functions檔案夾中,我做了npm install.
這是我的functions/package.json檔案:
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"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": "^9.8.0",
"firebase-functions": "^3.14.1",
"body-parser": "~1.0.1",
"express": "~4.0.0",
"tedious": "^14.3.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0",
"nodemon": "^2.0.15"
},
"private": true
}
然后唯一的firebase.json檔案有這些設定:
{
"hosting": {
"public": "public",
"rewrites": [
{
"source": "**",
"function": "app"
}
],
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
}
}
當我這樣做firebase deploy(部署功能和托管)時,或者firebase deploy --only functions我收到一個錯誤,我從中獲取了最后 10 行:
[debug] [2022-03-08T02:48:07.963Z] <<< [apiv2][body] DELETE https://us.gcr.io/v2/ventes-4f9b6/gcf/us-central1/053feedd-aed4-4c8d-93c4-591b134374b6/cache/manifests/sha256:7b2b71f239340ebec209e230e76e303b6fd7293c8f23ee3292f23d8cf4571319 {"errors":[]}
[debug] [2022-03-08T02:48:08.022Z] Error: Failed to update function app in region us-central1
at /usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:38:11
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Fabricator.updateV1Function (/usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:255:32)
at async Fabricator.updateEndpoint (/usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:136:13)
at async handle (/usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:75:17)
[error]
[error] Error: There was an error deploying functions
我嘗試了具有相似標題的不同解決方案,但到目前為止沒有任何效果。我也嘗試在functions檔案夾中再次安裝軟體包,但對我來說沒有任何問題。
uj5u.com熱心網友回復:
您不能在函式檔案夾之外擁有檔案。只有函式檔案夾中的內容才會被部署。將其移動到您的函式檔案夾中。
var connection = require('../utils/dbconfig'); //outside functions folder
此外,functions.https.onRequest句柄決議傳入請求的主體,因此使用任何主體決議器都可能導致錯誤,您應該將其洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/439744.html
標籤:javascript 节点.js 火力基地 谷歌云功能
