หากต้องการเริ่มต้นใช้งาน Cloud Functions ให้ลองทำตามบทแนะนำนี้ ซึ่งเริ่มต้นด้วยงานการตั้งค่าที่จำเป็น และดำเนินการต่อด้วยการสร้าง ทดสอบ และติดตั้งใช้งานฟังก์ชันที่เกี่ยวข้อง 2 รายการ
- ฟังก์ชัน "เพิ่มข้อความ" ที่แสดง URL ซึ่งยอมรับค่าข้อความและเขียนลงใน Cloud Firestore
- ฟังก์ชัน "เปลี่ยนเป็นตัวพิมพ์ใหญ่" ที่ทริกเกอร์เมื่อมีการCloud Firestoreเขียนและแปลง ข้อความเป็นตัวพิมพ์ใหญ่
ต่อไปนี้คือโค้ดตัวอย่างแบบเต็มที่มีฟังก์ชัน
Node.js
// The Cloud Functions for Firebase SDK to create Cloud Functions and triggers.
const {logger} = require("firebase-functions");
const {onRequest} = require("firebase-functions/https");
const {onDocumentCreated} = require("firebase-functions/firestore");
// The Firebase Admin SDK to access Firestore.
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");
initializeApp();
// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
exports.addmessage = onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into Firestore using the Firebase Admin SDK.
const writeResult = await getFirestore()
.collection("messages")
.add({original: original});
// Send back a message that we've successfully written the message
res.json({result: `Message with ID: ${writeResult.id} added.`});
});
// Listens for new messages added to /messages/:documentId/original
// and saves an uppercased version of the message
// to /messages/:documentId/uppercase
exports.makeuppercase = onDocumentCreated("/messages/{documentId}", (event) => {
// Grab the current value of what was written to Firestore.
const original = event.data.data().original;
// Access the parameter `{documentId}` with `event.params`
logger.log("Uppercasing", event.params.documentId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing
// asynchronous tasks inside a function
// such as writing to Firestore.
// Setting an 'uppercase' field in Firestore document returns a Promise.
return event.data.ref.set({uppercase}, {merge: true});
});
Python
# The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
from firebase_functions import firestore_fn, https_fn
# The Firebase Admin SDK to access Cloud Firestore.
from firebase_admin import initialize_app, firestore
import google.cloud.firestore
app = initialize_app()
@https_fn.on_request()
def addmessage(req: https_fn.Request) -> https_fn.Response:
"""Take the text parameter passed to this HTTP endpoint and insert it into
a new document in the messages collection."""
# Grab the text parameter.
original = req.args.get("text")
if original is None:
return https_fn.Response("No text parameter provided", status=400)
firestore_client: google.cloud.firestore.Client = firestore.client()
# Push the new message into Cloud Firestore using the Firebase Admin SDK.
_, doc_ref = firestore_client.collection("messages").add({"original": original})
# Send back a message that we've successfully written the message
return https_fn.Response(f"Message with ID {doc_ref.id} added.")
@firestore_fn.on_document_created(document="messages/{pushId}")
def makeuppercase(event: firestore_fn.Event[firestore_fn.DocumentSnapshot | None]) -> None:
"""Listens for new documents to be added to /messages. If the document has
an "original" field, creates an "uppercase" field containg the contents of
"original" in upper case."""
# Get the value of "original" if it exists.
if event.data is None:
return
try:
original = event.data.get("original")
except KeyError:
# No "original" field, so do nothing.
return
# Set the "uppercase" field.
print(f"Uppercasing {event.params['pushId']}: {original}")
upper = original.upper()
event.data.reference.update({"uppercase": upper})
เกี่ยวกับบทแนะนำนี้
เราเลือกฟังก์ชัน Cloud Firestore และฟังก์ชันที่ทริกเกอร์ด้วย HTTP สำหรับตัวอย่างนี้ เนื่องจากทริกเกอร์ในเบื้องหลังเหล่านี้สามารถทดสอบได้อย่างละเอียด ผ่าน Firebase Local Emulator Suite ชุดเครื่องมือนี้ ยังรองรับ Realtime Database, Cloud Storage, PubSub, Auth และทริกเกอร์ที่เรียกใช้ได้ผ่าน HTTP ทริกเกอร์ในเบื้องหลังประเภทอื่นๆ เช่น Remote Config และทริกเกอร์ TestLab สามารถทดสอบแบบอินเทอร์แอกทีฟได้โดยใช้ชุดเครื่องมือที่ไม่ได้อธิบายไว้ในหน้านี้
ส่วนต่อไปนี้ของบทแนะนำนี้จะอธิบายขั้นตอนที่จำเป็นในการสร้าง ทดสอบ และติดตั้งใช้งานตัวอย่าง
สร้างโปรเจ็กต์ Firebase
หากคุณเพิ่งเริ่มใช้ Firebase หรือ Cloud
ทำตามขั้นตอนต่อไปนี้หากคุณเพิ่งเริ่มใช้ Firebase หรือ Google Cloud
คุณยังทำตามขั้นตอนเหล่านี้ได้หากต้องการสร้างโปรเจ็กต์ Firebase ใหม่ทั้งหมด (และโปรเจ็กต์ Google Cloud ที่เกี่ยวข้อง)
- ลงชื่อเข้าใช้คอนโซล Firebase
- คลิกปุ่มเพื่อสร้างโปรเจ็กต์ Firebase ใหม่
-
ป้อนชื่อโปรเจ็กต์ในช่องข้อความ
หากเป็นส่วนหนึ่งของGoogle Cloud องค์กร คุณสามารถเลือกโฟลเดอร์ที่จะสร้างโปรเจ็กต์ได้
- หากมีข้อความแจ้ง ให้อ่านและยอมรับข้อกำหนดในการให้บริการของ Firebase แล้วคลิกต่อไป
- (ไม่บังคับ) เปิดใช้ความช่วยเหลือจาก AI ในคอนโซล Firebase (เรียกว่า "Gemini ใน Firebase") ซึ่งจะช่วยให้คุณเริ่มต้นใช้งานและ เพิ่มประสิทธิภาพกระบวนการพัฒนาได้
-
(ไม่บังคับ) ตั้งค่า Google Analytics สำหรับโปรเจ็กต์ ซึ่งจะช่วยให้ได้รับประสบการณ์การใช้งานผลิตภัณฑ์ Firebase เหล่านี้อย่างเหมาะสมที่สุด Firebase A/B Testing Cloud Messaging Crashlytics In-App Messaging และ Remote Config (รวมถึง การปรับเปลี่ยนในแบบของคุณ)
เลือกบัญชี Google Analytics ที่มีอยู่ หรือสร้างบัญชีใหม่ หากสร้างบัญชีใหม่ ให้เลือกAnalyticsสถานที่ตั้งการรายงาน จากนั้นยอมรับการตั้งค่าการแชร์ข้อมูลและGoogle Analyticsข้อกำหนด สำหรับโปรเจ็กต์
- คลิกสร้างโปรเจ็กต์
Firebase จะสร้างโปรเจ็กต์ จัดสรรทรัพยากรเริ่มต้นบางอย่าง และ เปิดใช้ API ที่สำคัญ เมื่อกระบวนการเสร็จสมบูรณ์ ระบบจะนำคุณไปยังหน้าภาพรวมของโปรเจ็กต์ Firebase ในFirebase คอนโซล
โปรเจ็กต์ที่อยู่ในระบบคลาวด์ที่มีอยู่
ทําตามขั้นตอนต่อไปนี้หากต้องการเริ่มใช้ Firebase กับโปรเจ็กต์ที่มีอยู่ Google Cloud ดูข้อมูลเพิ่มเติมและแก้ปัญหา "การเพิ่ม Firebase" ลงในโปรเจ็กต์ Google Cloud ที่มีอยู่
- ลงชื่อเข้าใช้Firebaseคอนโซลด้วยบัญชีที่ให้สิทธิ์เข้าถึงโปรเจ็กต์ Google Cloud ที่มีอยู่
- คลิกปุ่มเพื่อสร้างโปรเจ็กต์ Firebase ใหม่
- ที่ด้านล่างของหน้า ให้คลิก เพิ่ม Firebase ลงในโปรเจ็กต์ Google Cloud
- ในช่องข้อความ ให้เริ่มป้อนชื่อโปรเจ็กต์ของ โปรเจ็กต์ที่มีอยู่ แล้วเลือกโปรเจ็กต์จากรายการที่แสดง
- คลิกเปิดโปรเจ็กต์
- หากมีข้อความแจ้ง ให้อ่านและยอมรับข้อกำหนดในการให้บริการของ Firebase แล้วคลิกต่อไป
- (ไม่บังคับ) เปิดใช้ความช่วยเหลือจาก AI ในคอนโซล Firebase (เรียกว่า "Gemini ใน Firebase") ซึ่งจะช่วยให้คุณเริ่มต้นใช้งานและ เพิ่มประสิทธิภาพกระบวนการพัฒนาได้
-
(ไม่บังคับ) ตั้งค่า Google Analytics สำหรับโปรเจ็กต์ ซึ่งจะช่วยให้ได้รับประสบการณ์การใช้งานผลิตภัณฑ์ Firebase เหล่านี้อย่างเหมาะสมที่สุด Firebase A/B Testing Cloud Messaging Crashlytics In-App Messaging และ Remote Config (รวมถึง การปรับเปลี่ยนในแบบของคุณ)
เลือกบัญชี Google Analytics ที่มีอยู่ หรือสร้างบัญชีใหม่ หากสร้างบัญชีใหม่ ให้เลือกAnalyticsสถานที่ตั้งการรายงาน จากนั้นยอมรับการตั้งค่าการแชร์ข้อมูลและGoogle Analyticsข้อกำหนด สำหรับโปรเจ็กต์
- คลิกเพิ่ม Firebase
Firebase เพิ่ม Firebase ไปยังโปรเจ็กต์ที่มีอยู่ เมื่อกระบวนการเสร็จสมบูรณ์ ระบบจะนำคุณไปยังหน้าภาพรวมของโปรเจ็กต์ Firebase ในFirebaseคอนโซล
ตั้งค่าสภาพแวดล้อมและ Firebase CLI
Node.js
คุณจะต้องมีสภาพแวดล้อม Node.js เพื่อเขียนฟังก์ชัน และต้องใช้ Firebase CLI เพื่อติดตั้งใช้งานฟังก์ชันในรันไทม์ Cloud Functions ขอแนะนำให้ใช้ Node Version Manager ในการติดตั้ง Node.js และ npm
เมื่อติดตั้ง Node.js และ npm แล้ว ให้ติดตั้ง Firebase CLI ด้วยวิธีที่คุณต้องการ หากต้องการติดตั้ง CLI ผ่าน npm ให้ใช้คำสั่งต่อไปนี้
npm install -g firebase-tools
ซึ่งจะติดตั้งคำสั่ง firebase ที่พร้อมใช้งานทั่วโลก หาก
คำสั่งล้มเหลว คุณอาจต้อง
เปลี่ยนสิทธิ์ npm
หากต้องการอัปเดตเป็น firebase-tools เวอร์ชันล่าสุด ให้เรียกใช้คำสั่งเดิมอีกครั้ง
Python
คุณจะต้องมีสภาพแวดล้อม Python
เพื่อเขียนฟังก์ชัน
และจะต้องมี Firebase CLI เพื่อติดตั้งใช้งานฟังก์ชันใน
รันไทม์ Cloud Functions เราขอแนะนำให้ใช้ venv เพื่อ
แยกการอ้างอิง รองรับ Python เวอร์ชัน 3.10 ถึง 3.13 โดย 3.13 เป็นรันไทม์เริ่มต้น
เมื่อติดตั้ง Python แล้ว ให้ติดตั้ง Firebase CLI ด้วยวิธีที่คุณต้องการ
เริ่มต้นโปรเจ็กต์
เมื่อเริ่มต้น Firebase SDK สำหรับ Cloud Functions คุณจะสร้างโปรเจ็กต์ที่ว่างเปล่า ซึ่งมีทรัพยากร Dependency และโค้ดตัวอย่างขั้นต่ำบางส่วน หากใช้ Node.js คุณสามารถเลือกใช้ TypeScript หรือ JavaScript เพื่อเขียนฟังก์ชันได้ สำหรับวัตถุประสงค์ของ บทแนะนำนี้ คุณจะต้องเริ่มต้น Cloud Firestore ด้วย
วิธีเริ่มต้นโปรเจ็กต์
- เรียกใช้
firebase loginเพื่อเข้าสู่ระบบผ่านเบราว์เซอร์และตรวจสอบสิทธิ์ Firebase CLI - ไปที่ไดเรกทอรีโปรเจ็กต์ Firebase
- วิ่ง
firebase init firestoreสำหรับบทแนะนำนี้ คุณสามารถยอมรับค่าเริ่มต้น เมื่อระบบแจ้งให้ระบุกฎ Firestore และไฟล์ดัชนี หากยังไม่ได้ใช้ Cloud Firestore ในโปรเจ็กต์นี้ คุณจะต้อง เลือกโหมดเริ่มต้นและตำแหน่งสำหรับ Firestore ตามที่อธิบายไว้ใน เริ่มต้นใช้งาน Cloud Firestore - วิ่ง
firebase init functionsCLI จะแจ้งให้คุณเลือกโค้ดเบสที่มีอยู่ หรือเริ่มต้นและตั้งชื่อโค้ดเบสใหม่ เมื่อเพิ่งเริ่มต้น ฐานของโค้ดเดียวในตำแหน่งเริ่มต้นก็เพียงพอ ต่อมาเมื่อการติดตั้งใช้งานขยายออกไป คุณอาจ ต้องการจัดระเบียบฟังก์ชันในฐานของโค้ด CLI มีตัวเลือกต่อไปนี้สำหรับการรองรับภาษา
- JavaScript
- TypeScript
- Python
สำหรับบทแนะนำนี้ ให้เลือก JavaScript หรือ Python หากต้องการเขียนฟังก์ชันใน TypeScript โปรดดูเขียนฟังก์ชันด้วย TypeScript
CLI ให้ตัวเลือกในการติดตั้งทรัพยากร Dependency คุณปฏิเสธได้ หากต้องการจัดการการอ้างอิงด้วยวิธีอื่น
หลังจากคำสั่งเหล่านี้ทำงานเสร็จเรียบร้อยแล้ว โครงสร้างโปรเจ็กต์จะมีลักษณะดังนี้
Node.js
myproject
+- .firebaserc # Hidden file that helps you quickly switch between
| # projects with `firebase use`
|
+- firebase.json # Describes properties for your project
|
+- functions/ # Directory containing all your functions code
|
+- .eslintrc.json # Optional file containing rules for JavaScript linting.
|
+- package.json # npm package file describing your Cloud Functions code
|
+- index.js # Main source file for your Cloud Functions code
|
+- node_modules/ # Directory where your dependencies (declared in
# package.json) are installed
สำหรับ Node.js ไฟล์ package.json ที่สร้างขึ้นระหว่างการเริ่มต้นจะมีคีย์ที่สำคัญ
คือ "engines": {"node": "18"} ซึ่งจะระบุเวอร์ชัน Node.js สำหรับ
การเขียนและติดตั้งใช้งานฟังก์ชัน คุณสามารถเลือกเวอร์ชันอื่นๆ ที่รองรับได้
Python
myproject
+- .firebaserc # Hidden file that helps you quickly switch between
| # projects with `firebase use`
|
+- firebase.json # Describes properties for your project
|
+- functions/ # Directory containing all your functions code
|
+- main.py # Main source file for your Cloud Functions code
|
+- requirements.txt # List of the project's modules and packages
|
+- venv/ # Directory where your dependencies are installed
นำเข้าโมดูลที่จำเป็นและเริ่มต้นแอป
หลังจากทํางานการตั้งค่าเสร็จแล้ว คุณจะ เปิดไดเรกทอรีต้นทางและเริ่มเพิ่มโค้ดตามที่อธิบายไว้ใน ส่วนต่อไปนี้ได้ สำหรับตัวอย่างนี้ โปรเจ็กต์ของคุณต้องนำเข้าโมดูล Cloud Functionsและ Admin SDK เพิ่มบรรทัด เช่นบรรทัดต่อไปนี้ลงในไฟล์ต้นฉบับ
Node.js
// The Cloud Functions for Firebase SDK to create Cloud Functions and triggers.
const {logger} = require("firebase-functions");
const {onRequest} = require("firebase-functions/https");
const {onDocumentCreated} = require("firebase-functions/firestore");
// The Firebase Admin SDK to access Firestore.
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");
initializeApp();
Python
# The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
from firebase_functions import firestore_fn, https_fn
# The Firebase Admin SDK to access Cloud Firestore.
from firebase_admin import initialize_app, firestore
import google.cloud.firestore
app = initialize_app()
บรรทัดเหล่านี้จะโหลดโมดูลที่จำเป็นและ
เริ่มต้นadminอินสแตนซ์แอปซึ่งสามารถทำการเปลี่ยนแปลงCloud Firestoreได้
เมื่อใดก็ตามที่มีการรองรับ Admin SDK เช่นเดียวกับ
สำหรับ FCM, Authentication และ Firebase Realtime Database ก็จะช่วยให้คุณผสานรวม Firebase ได้อย่างมีประสิทธิภาพโดยใช้ Cloud Functions
Firebase CLI จะติดตั้ง Firebase Admin SDK และ Firebase SDK สำหรับโมดูล Cloud Functions โดยอัตโนมัติเมื่อคุณเริ่มต้นโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับการเพิ่มไลบรารีของบุคคลที่สาม ลงในโปรเจ็กต์ได้ที่ จัดการการขึ้นต่อกัน
เพิ่มฟังก์ชัน "เพิ่มข้อความ"
สำหรับฟังก์ชัน "add message" ให้เพิ่มบรรทัดต่อไปนี้ลงในไฟล์ต้นฉบับ
Node.js
// Take the text parameter passed to this HTTP endpoint and insert it into
// Firestore under the path /messages/:documentId/original
exports.addmessage = onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into Firestore using the Firebase Admin SDK.
const writeResult = await getFirestore()
.collection("messages")
.add({original: original});
// Send back a message that we've successfully written the message
res.json({result: `Message with ID: ${writeResult.id} added.`});
});
Python
@https_fn.on_request()
def addmessage(req: https_fn.Request) -> https_fn.Response:
"""Take the text parameter passed to this HTTP endpoint and insert it into
a new document in the messages collection."""
# Grab the text parameter.
original = req.args.get("text")
if original is None:
return https_fn.Response("No text parameter provided", status=400)
firestore_client: google.cloud.firestore.Client = firestore.client()
# Push the new message into Cloud Firestore using the Firebase Admin SDK.
_, doc_ref = firestore_client.collection("messages").add({"original": original})
# Send back a message that we've successfully written the message
return https_fn.Response(f"Message with ID {doc_ref.id} added.")
ฟังก์ชัน "add message" เป็นปลายทาง HTTP คำขอใดๆ ไปยังปลายทาง
จะส่งผลให้มีการส่งออบเจ็กต์คำขอและการตอบกลับไปยัง
ตัวแฮนเดิลคำขอสำหรับแพลตฟอร์มของคุณ (onRequest()
หรือ on_request)
ฟังก์ชัน HTTP เป็นแบบซิงโครนัส (คล้ายกับฟังก์ชันที่เรียกใช้ได้) ดังนั้นคุณควรส่งการตอบกลับโดยเร็วที่สุดและเลื่อนงานโดยใช้ Cloud Firestore ฟังก์ชัน HTTP "add message"
จะส่งค่าข้อความไปยังปลายทาง HTTP และแทรกลงใน
ฐานข้อมูลภายใต้เส้นทาง /messages/:documentId/original
เพิ่มฟังก์ชัน "เปลี่ยนเป็นตัวพิมพ์ใหญ่"
สำหรับฟังก์ชัน "เปลี่ยนเป็นตัวพิมพ์ใหญ่" ให้เพิ่มบรรทัดต่อไปนี้ลงในไฟล์ต้นฉบับ
Node.js
// Listens for new messages added to /messages/:documentId/original
// and saves an uppercased version of the message
// to /messages/:documentId/uppercase
exports.makeuppercase = onDocumentCreated("/messages/{documentId}", (event) => {
// Grab the current value of what was written to Firestore.
const original = event.data.data().original;
// Access the parameter `{documentId}` with `event.params`
logger.log("Uppercasing", event.params.documentId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing
// asynchronous tasks inside a function
// such as writing to Firestore.
// Setting an 'uppercase' field in Firestore document returns a Promise.
return event.data.ref.set({uppercase}, {merge: true});
});
Python
@firestore_fn.on_document_created(document="messages/{pushId}")
def makeuppercase(event: firestore_fn.Event[firestore_fn.DocumentSnapshot | None]) -> None:
"""Listens for new documents to be added to /messages. If the document has
an "original" field, creates an "uppercase" field containg the contents of
"original" in upper case."""
# Get the value of "original" if it exists.
if event.data is None:
return
try:
original = event.data.get("original")
except KeyError:
# No "original" field, so do nothing.
return
# Set the "uppercase" field.
print(f"Uppercasing {event.params['pushId']}: {original}")
upper = original.upper()
event.data.reference.update({"uppercase": upper})
ฟังก์ชัน "เปลี่ยนเป็นตัวพิมพ์ใหญ่" จะทำงานเมื่อมีการเขียน Cloud Firestore ไปยัง ซึ่งกำหนดเอกสารที่จะรับฟัง ด้วยเหตุผลด้านประสิทธิภาพ คุณ ควรระบุให้เฉพาะเจาะจงที่สุด
วงเล็บปีกกา เช่น {documentId} จะครอบ "พารามิเตอร์" ไวลด์การ์ด
ที่แสดงข้อมูลที่ตรงกันใน Callback Cloud Firestore จะทริกเกอร์
การเรียกกลับทุกครั้งที่มีการเพิ่มข้อความใหม่
ใน Node.js ฟังก์ชันที่ขับเคลื่อนด้วยเหตุการณ์ เช่น Cloud Firestore เหตุการณ์จะ
ทำงานแบบไม่พร้อมกัน ฟังก์ชัน Callback ควรแสดงผล null, ออบเจ็กต์ หรือ Promise
หากคุณไม่ส่งคืนอะไรเลย ฟังก์ชันจะหมดเวลา ซึ่งเป็นการส่งสัญญาณข้อผิดพลาด และ
จะลองอีกครั้ง ดูSync, Async และ Promises
จำลองการดำเนินการของฟังก์ชัน
Firebase Local Emulator Suite ช่วยให้คุณสร้างและทดสอบแอปในเครื่องแทนการทําให้ใช้งานได้ใน โปรเจ็กต์ Firebase เราขอแนะนำอย่างยิ่งให้ทำการทดสอบในเครื่องระหว่างการพัฒนาซอฟต์แวร์ เนื่องจากจะช่วยลดความเสี่ยงจากข้อผิดพลาดในการเขียนโค้ดที่อาจทำให้เกิดค่าใช้จ่ายในสภาพแวดล้อมฮาร์ดแวร์และซอฟต์แวร์ (เช่น ลูปที่ไม่มีที่สิ้นสุด)
วิธีจำลองฟังก์ชัน
เรียกใช้
firebase emulators:startและตรวจสอบเอาต์พุตสำหรับ URL ของ Emulator Suite UI โดยค่าเริ่มต้นจะเป็น localhost:4000 แต่อาจโฮสต์ในพอร์ตอื่นในเครื่องของคุณ ป้อน URL นั้นในเบราว์เซอร์เพื่อเปิด Emulator Suite UIตรวจสอบเอาต์พุตของคำสั่ง
firebase emulators:startสำหรับ URL ของฟังก์ชัน HTTP โดยจะมีลักษณะคล้ายกับhttp://localhost:5001/MY_PROJECT/us-central1/addMessageยกเว้น- ระบบจะแทนที่
MY_PROJECTด้วยรหัสโปรเจ็กต์ของคุณ - พอร์ตอาจแตกต่างกันในเครื่องของคุณ
- ระบบจะแทนที่
เพิ่มสตริงการค้นหา
?text=uppercasemeที่ท้าย URL ของฟังก์ชัน โดยจะมีลักษณะดังนี้http://localhost:5001/MY_PROJECT/us-central1/addMessage?text=uppercasemeคุณจะเปลี่ยนข้อความ "uppercaseme" เป็นข้อความที่กำหนดเองก็ได้สร้างข้อความใหม่โดยเปิด URL ในแท็บใหม่ในเบราว์เซอร์
ดูผลลัพธ์ของฟังก์ชันใน Emulator Suite UI
ในแท็บบันทึก คุณควรเห็นบันทึกใหม่ที่ระบุว่าฟังก์ชัน HTTP ทำงานสำเร็จแล้ว ดังนี้
i functions: Beginning execution of "addMessage"i functions: Beginning execution of "makeUppercase"ในแท็บ Firestore คุณควรเห็นเอกสารที่มีข้อความต้นฉบับ รวมถึงข้อความเวอร์ชันตัวพิมพ์ใหญ่ (หากเดิมเป็น "uppercaseme" คุณจะเห็น "UPPERCASEME")
ทำให้ฟังก์ชันใช้งานได้ในสภาพแวดล้อมฮาร์ดแวร์และซอฟต์แวร์
เมื่อฟังก์ชันทำงานได้ตามที่ต้องการในโปรแกรมจำลองแล้ว คุณก็สามารถดำเนินการติดตั้งใช้งาน ทดสอบ และเรียกใช้ฟังก์ชันในสภาพแวดล้อมฮาร์ดแวร์และซอฟต์แวร์ได้ โปรดทราบว่าหากต้องการ นำไปใช้งานจริง โปรเจ็กต์ของคุณ ต้องอยู่ในแพ็กเกจราคา Blaze ดู Cloud Functions ราคา
หากต้องการทำบทแนะนำให้เสร็จสมบูรณ์ ให้ติดตั้งใช้งานฟังก์ชัน แล้วเรียกใช้ ฟังก์ชันเหล่านั้น
เรียกใช้คำสั่งนี้เพื่อติดตั้งใช้งานฟังก์ชัน
firebase deploy --only functions
หลังจากเรียกใช้คำสั่งนี้แล้ว Firebase CLI จะแสดงผล URL สำหรับปลายทางฟังก์ชัน HTTP ทั้งหมด ในเทอร์มินัล คุณควรเห็นบรรทัดต่อไปนี้
Function URL (addMessage): https://us-central1-MY_PROJECT.cloudfunctions.net/addMessageURL มีรหัสโปรเจ็กต์และภูมิภาคสำหรับฟังก์ชัน HTTP แม้ว่าคุณจะไม่ต้องกังวลเกี่ยวกับเรื่องนี้ในตอนนี้ แต่ฟังก์ชัน HTTP บางอย่างในเวอร์ชันที่ใช้งานจริงควรระบุตำแหน่งเพื่อลดเวลาในการตอบสนองของเครือข่าย
หากพบข้อผิดพลาดในการเข้าถึง เช่น "ให้สิทธิ์เข้าถึงโปรเจ็กต์ไม่ได้" ให้ลองตรวจสอบการใช้นามแฝงของโปรเจ็กต์
ใช้ URL ที่ CLI แสดงผล เพิ่มพารามิเตอร์การค้นหาข้อความ และเปิดในเบราว์เซอร์
https://us-central1-MY_PROJECT.cloudfunctions.net/addMessage?text=uppercasemetooฟังก์ชันจะดำเนินการและเปลี่ยนเส้นทางเบราว์เซอร์ไปยัง Firebase คอนโซลที่ตำแหน่งฐานข้อมูล ซึ่งจัดเก็บสตริงข้อความ เหตุการณ์การเขียนนี้ จะทริกเกอร์ฟังก์ชัน "เปลี่ยนเป็นตัวพิมพ์ใหญ่" ซึ่งจะเขียนสตริง เวอร์ชันตัวพิมพ์ใหญ่
หลังจากติดตั้งใช้งานและเรียกใช้ฟังก์ชันแล้ว คุณจะดูบันทึกได้ในGoogle Cloud คอนโซล หากต้องการลบฟังก์ชัน ที่อยู่ระหว่างการพัฒนาหรือในเวอร์ชันที่ใช้งานจริง ให้ใช้ Firebase CLI
ในเวอร์ชันที่ใช้งานจริง คุณอาจต้องการเพิ่มประสิทธิภาพฟังก์ชันและควบคุม ค่าใช้จ่ายโดยการตั้งค่าจำนวนอินสแตนซ์ขั้นต่ำและสูงสุดที่จะเรียกใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวเลือกการรันไทม์เหล่านี้ได้ที่ ควบคุมลักษณะการทำงานของการปรับขนาด
ขั้นตอนถัดไป
ในเอกสารประกอบนี้ คุณสามารถดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีจัดการฟังก์ชันสำหรับ Cloud Functions รวมถึงวิธี จัดการกิจกรรมทุกประเภทที่ Cloud Functions รองรับ
หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับ Cloud Functions คุณ ยังทำสิ่งต่อไปนี้ได้ด้วย
- อ่านเกี่ยวกับกรณีการใช้งานสำหรับ Cloud Functions
- ลองใช้ Cloud Functions Codelab
- ตรวจสอบและเรียกใช้ตัวอย่างโค้ดใน GitHub