【问题标题】:Firebase CLI not deploying any Cloud FunctionsFirebase CLI 未部署任何云功能
【发布时间】:2022-02-09 06:36:23
【问题描述】:

我有一个 Angular 项目,我正在尝试部署单个 firebase 功能。

这是我的函数目录的样子:

当我使用命令firebase deploy --only functions 部署这些功能时,输出看起来正常且没有错误:

PS C:\Users\project-directory> firebase deploy --only functions

=== Deploying to 'firebase-project-name'...

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

> build
> 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 cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
i  functions: cleaning up build files...

+  Deploy complete!

Project Console: https://console.firebase.google.com/project/project-name/overview

src/user/index.ts 包含我要部署的功能的文件:

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

// import * as functions from "firebase-functions";
// import * as admin from 'firebase-admin';

const FieldValue = require('firebase-admin').firestore.FieldValue;

const db = admin.firestore();

/**
 * Add user to firestore
 */
export const createProfile = async (userRecord: any) => {
    const uid = userRecord.uid;
    const admin = false;
    const email = userRecord.email;
    const photoURL = userRecord.photoUrl || 'enter shortened url for default image';
    const name = userRecord.displayName || 'New User';
    const spouse = userRecord.spouse || 'TBA';
    const forumUserName = userRecord.forumUserName || 'New Username set by admin';
    const address = userRecord.address || 'TBA';
    const suburb = userRecord.suburb || 'TBA';
    const state = userRecord.state || 'QLD';
    const postCode = userRecord.postCode || '2000';
    const homePhone = userRecord.homePhone || '02 1234 5678';
    const mobilePhone = userRecord.mobilePhone || '0400 123 456';
    const memNum = userRecord.memNum || 123;
    const timestamp = FieldValue.serverTimestamp();
    const memType = userRecord.memType || 'Nominated';
    const memStatus = userRecord.memStatus || `Pending`;
    const isStateCoord = userRecord.isStateCoord || false;
    const stateCoordState = userRecord.stateCoordState || 'QLD';
    //const newUserRef = db.doc(`users/${uid}`)

    // Convert any date to timestamp for consistency

    try {
        return await db
            .collection(`users`)
            .doc(userRecord.uid)
            .set({
                uid: uid,
                email: email,
                photoURL: photoURL,
                fullName: name,
                mDOB: timestamp,
                spouse: spouse,
                sDOB: timestamp,
                forumUserName: forumUserName,
                address: address,
                suburb: suburb,
                state: state,
                postCode: postCode,
                homePhone: homePhone,
                mobilePhone: mobilePhone,
                memNum: memNum,
                memType: memType,
                memStatus: memStatus,
                memDueDate: timestamp,
                lastLoginDate: timestamp,
                joined: timestamp,
                updated: timestamp,
                admin: admin,
                isAdmin: admin,
                isStateCoord: isStateCoord,
                stateCoordState: stateCoordState,
            });
    } catch (message) {
        return console.error(message);
    }
};

exports.authOnCreate = functions.auth.user().onCreate(createProfile);

src/index.ts文件导入上述文件:

import * as user from './user';
export const createProfile = user.createProfile

问题是我没有看到该函数出现在 Firebase 控制台中。

我忽略了什么?

【问题讨论】:

  • 你需要声明‘functions.https.onRequest’。并将您的代码放入此方法中。 firebase.google.com/docs/functions/get-started查看第五章
  • 感谢 chichi 提供的有用链接。但是,这不是我在src/user/index.ts 文件中对这行代码所做的吗:exports.authOnCreate = functions.auth.user().onCreate(createProfile);

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


【解决方案1】:

src/user/index.ts 中有两种不同的导出语法:

export const createProfile = async (userRecord: any) => { /* ... */ }
exports.authOnCreate = functions.auth.user().onCreate(createProfile);

使用一个,或另一个,而不是两者。它们彼此不兼容。

export const createProfile = async (userRecord: any) => { /* ... */ }
export const authOnCreate = functions.auth.user().onCreate(createProfile);

然后在您的主 src/index.ts 中,导入 Cloud Function 导出,而不是普通函数:

import { authOnCreate } from './user'; // import specific parts for the best performance when transpiled
export const createProfile = authOnCreate;

【讨论】:

  • 谢谢你,samthecodingman,我在你回答的同时也意识到了同样的事情。不过,感谢您并指出语法错误!
【解决方案2】:

在我为您发布的原始代码中,createProfile 是 const 而不是导出。

开箱即用,应该可以正常工作。我看到的唯一区别是您已将代码放入用户子目录中。

再一次没有问题,而且可读性可能还可以。

我的做法可能不太好,但我的所有功能都在一个文件中。即使安装了扩展,它们也会进入同一个 index.js(ts) 文件。然后,如果我修改一个函数,我会使用firebase deploy --only functions:functionName

【讨论】:

  • 感谢您的提示,迈克尔。我觉得你的方法很好。如果我没有模块化代码,我就不会有上述问题。所以有一点需要担心的文件/代码更少。话虽如此,我还是遵循了建议将功能分组到逻辑域中的 Google 指南。
猜你喜欢
  • 2018-08-09
  • 2020-11-02
  • 2021-12-01
  • 2021-11-17
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
相关资源
最近更新 更多