我正在嘗試從 firestore 事件(onWrite)觸發云功能,但我沒有找到正確的方法來使用我用于該專案的版本 9 模塊化來實作它。整個檔案是版本 8(命名空間)。
這是我正在嘗試做的(版本 8):
export const documentWriteListener = functions.firestore
.document('collection/{documentUid}')
.onWrite((change, context) => {
if (!change.before.exists) {
// New document Created : add one to count
db.doc(docRef).update({ numberOfDocs: FieldValue.increment(1) });
} else if (change.before.exists && change.after.exists) {
// Updating existing document : Do nothing
} else if (!change.after.exists) {
// Deleting document : subtract one from count
db.doc(docRef).update({ numberOfDocs: FieldValue.increment(-1) });
}
return;
});
這是我的第 9 版 firebase 初始化檔案:
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth";
import { getFunctions } from "firebase/functions"
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = getAuth(app);
const functions = getFunctions(app);
export { db, auth, functions }
這是我為云功能嘗試過的(我把它放在一個獨立的檔案 actionsCount.js 中):
import { db, functions } from '../../firebase/initFirebase';
import { updateDoc, doc } from "firebase/firestore";
import * as functions from 'firebase-functions';
export const documentWriteListeners = functions.firestore
.document('actions/{documentUid}')
.onWrite((change, context) => {
const actionsCounter = doc(db, "actionsCount", "counter")
if (!change.before.exists()) {
// New document Created : add one to count
await updateDoc(actionsCounter, { numberOfDocs: FieldValue.increment(1) });
} else if (change.before.exists() && change.after.exists()) {
// Updating existing document : Do nothing
} else if (!change.after.exists()) {
// Deleting document : subtract one from count
await updateDoc(actionsCounter, { numberOfDocs: FieldValue.increment(-1) });
}
return;
})
當我使用 部署時firebase deploy --only functions
,我收到錯誤:無法理解要部署/服務的目標。firebase.json 中沒有目標匹配 '--only functions'。
我應該把這個功能放在別的地方嗎?功能不對?
非常感謝你的幫助 !
uj5u.com熱心網友回復:
您需要使用命令初始化 Firebase 函式firebase init
。當您選擇functions
并繼續設定時,它將index.js/ts
默認創建一個包含檔案的新函式目錄。這就是您添加 Cloud Functions 的地方。
firebase-functions
SDK 不適合在客戶端使用。
目錄結構如下所示:
firebase deploy --only functions
將部署功能。
uj5u.com熱心網友回復:
終于解決了這個問題!半天哈哈所以我顯然按照 Dharmaraj 的建議運行了 firebase init 函式。根據模塊化(第 9 版),看起來我們可以做到。因此,我基本上嘗試使用版本 8(命名空間)方法在 functions/index.js 檔案中初始化 firebase sdk,并且它起作用了。這是代碼:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
const fieldValue = admin.firestore.FieldValue;
// Updating the Actions document count
exports.documentWriteListeners = functions.firestore.document('actions/{documentUid}').onWrite((change, context) => {
if (!change.before.exists) {
// New document Created : add one to count
db.doc('actionsCount/counter').update({ numberOfDocs: fieldValue.increment(1) });
} else if (change.before.exists && change.after.exists) {
// Updating existing document : Do nothing
} else if (!change.after.exists) {
// Deleting document : subtract one from count
db.doc('actionsCount/counter').update({ numberOfDocs: fieldValue.increment(-1) });
}
return;
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492835.html