Firebase CLI 未部署任何 Cloud Functions
Firebase CLI not deploying any Cloud Functions
我有一个 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 控制台中看到该函数。
我忽略了什么?
您在 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;
在我为您发布的原始代码中,createProfile 是一个 const 而不是导出。
开箱即用,应该可以正常使用。我看到的唯一区别是您已将代码放入用户子目录中。
同样没有问题,可读性可能还不错。
我的做法可能不太好,但我的所有功能都在一个文件中。即使安装了扩展,它们也会进入同一个 index.js(ts) 文件。然后,如果我修改一个函数,我会使用 firebase deploy --only functions:functionName
我有一个 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 控制台中看到该函数。
我忽略了什么?
您在 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;
在我为您发布的原始代码中,createProfile 是一个 const 而不是导出。
开箱即用,应该可以正常使用。我看到的唯一区别是您已将代码放入用户子目录中。
同样没有问题,可读性可能还不错。
我的做法可能不太好,但我的所有功能都在一个文件中。即使安装了扩展,它们也会进入同一个 index.js(ts) 文件。然后,如果我修改一个函数,我会使用 firebase deploy --only functions:functionName