เริ่มต้นใช้งาน: เขียน ทดสอบ และทำให้ฟังก์ชันแรกใช้งานได้


หากต้องการเริ่มต้นใช้งาน 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/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/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 ชุดเครื่องมือนี้ยังรองรับทริกเกอร์สำหรับเรียกใช้ฐานข้อมูลแบบเรียลไทม์, Cloud Storage, PubSub, Auth และ HTTP ได้ด้วย คุณทดสอบแบบอินเทอร์แอกทีฟได้โดยใช้ชุดเครื่องมือที่ไม่ได้อธิบายในหน้านี้ เช่น ทริกเกอร์การกำหนดค่าระยะไกลและทริกเกอร์ TestLab

ส่วนต่อไปนี้ของบทแนะนำนี้จะอธิบายขั้นตอนที่จำเป็นในการสร้าง ทดสอบ และทำให้ตัวอย่างใช้งานได้

สร้างโปรเจ็กต์ Firebase

  1. ในคอนโซล Firebase ให้คลิกเพิ่มโปรเจ็กต์

    • หากต้องการเพิ่มทรัพยากร Firebase ไปยังโปรเจ็กต์ Google Cloud ที่มีอยู่ ให้ป้อนชื่อโปรเจ็กต์หรือเลือกจากเมนูแบบเลื่อนลง

    • หากต้องการสร้างโปรเจ็กต์ใหม่ ให้ป้อนชื่อโปรเจ็กต์ที่ต้องการ นอกจากนี้ คุณยังแก้ไขรหัสโปรเจ็กต์ที่แสดงอยู่ใต้ชื่อโปรเจ็กต์ได้อีกด้วย

  2. อ่านและยอมรับข้อกำหนดของ Firebase หากได้รับข้อความแจ้ง

  3. คลิกดำเนินการต่อ

  4. (ไม่บังคับ) ตั้งค่า Google Analytics สําหรับโปรเจ็กต์ ซึ่งจะช่วยให้คุณได้รับประสบการณ์การใช้งานที่ดีที่สุดเมื่อใช้ผลิตภัณฑ์ Firebase ต่อไปนี้

    เลือกบัญชี Google Analytics ที่มีอยู่หรือสร้างบัญชีใหม่

    หากคุณสร้างบัญชีใหม่ ให้เลือกตำแหน่งในการรายงาน Analytics แล้วยอมรับการตั้งค่าการแชร์ข้อมูลและข้อกำหนดของ Google Analytics สำหรับโปรเจ็กต์ของคุณ

  5. คลิกสร้างโปรเจ็กต์ (หรือเพิ่ม Firebase หากคุณใช้โปรเจ็กต์ Google Cloud ที่มีอยู่)

Firebase จะจัดสรรทรัพยากรสำหรับโปรเจ็กต์ Firebase ของคุณโดยอัตโนมัติ เมื่อขั้นตอนเสร็จสมบูรณ์แล้ว ระบบจะนำคุณไปยังหน้าภาพรวมสำหรับโปรเจ็กต์ Firebase ในคอนโซล Firebase

ตั้งค่าสภาพแวดล้อมและ Firebase CLI

Node.js

คุณจะต้องมีสภาพแวดล้อม Node.js เพื่อเขียนฟังก์ชัน และคุณจะต้องมี Firebase CLI เพื่อทำให้ฟังก์ชันใช้งานได้ในรันไทม์ของ Cloud Functions สำหรับการติดตั้ง 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 เพื่อแยกทรัพยากร Dependency รองรับ Python เวอร์ชัน 3.10 และ 3.11

เมื่อติดตั้ง Python แล้ว ให้ติดตั้ง Firebase CLI ผ่านวิธีการที่ต้องการ

เริ่มต้นโปรเจ็กต์

เมื่อเริ่มต้น Firebase SDK สำหรับ Cloud Functions คุณจะสร้างโปรเจ็กต์เปล่าที่มีทรัพยากร Dependency และโค้ดตัวอย่างบางส่วนน้อยที่สุด หากคุณใช้ Node.js จะเลือก TypeScript หรือ JavaScript สำหรับเขียนฟังก์ชันก็ได้ สำหรับวัตถุประสงค์ของบทแนะนำนี้ คุณจะต้องเริ่มต้น Cloud Firestore ด้วย

วิธีเริ่มต้นโปรเจ็กต์

  1. เรียกใช้ firebase login เพื่อลงชื่อเข้าสู่ระบบผ่านเบราว์เซอร์และตรวจสอบสิทธิ์ Firebase CLI
  2. ไปที่ไดเรกทอรีโปรเจ็กต์ Firebase
  3. เรียกใช้ firebase init firestore สำหรับบทแนะนำนี้ คุณสามารถยอมรับค่าเริ่มต้นเมื่อได้รับข้อความแจ้งเกี่ยวกับกฎ Firestore และไฟล์จัดทำดัชนี หากยังไม่เคยใช้ Cloud Firestore ในโปรเจ็กต์นี้ คุณจะต้องเลือกโหมดเริ่มต้นและตำแหน่งสำหรับ Firestore ด้วยตามที่อธิบายไว้ในเริ่มต้นใช้งาน Cloud Firestore
  4. เรียกใช้ firebase init functions CLI จะแจ้งให้คุณเลือกโค้ดเบสที่มีอยู่ หรือเริ่มต้นและตั้งชื่อโค้ดใหม่ เมื่อคุณเพิ่งเริ่มต้นใช้งาน ฐานของโค้ดเพียงฐานเดียวในตำแหน่งเริ่มต้นก็เพียงพอแล้ว หลังจากนั้นเมื่อการนำไปใช้งานขยาย คุณอาจต้องจัดระเบียบฟังก์ชันในฐานของโค้ด
  5. CLI มีตัวเลือกต่อไปนี้สำหรับการสนับสนุนภาษา

    • JavaScript
    • TypeScript
    • Python

    สำหรับบทแนะนำนี้ ให้เลือก JavaScript หรือ Python สำหรับการเขียนใน TypeScript โปรดดูที่เขียนฟังก์ชันด้วย TypeScript

  6. CLI จะมีตัวเลือกให้คุณติดตั้งทรัพยากร Dependency คุณสามารถปฏิเสธได้หากคุณต้องการจัดการทรัพยากร 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/v2/https");
const {onDocumentCreated} = require("firebase-functions/v2/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 ในทุกที่ที่การรองรับ SDK ผู้ดูแลระบบมีให้บริการสำหรับ FCM, การตรวจสอบสิทธิ์ และฐานข้อมูลเรียลไทม์ของ Firebase ก็เป็นวิธีที่ทรงพลังในการผสานรวม Firebase โดยใช้ Cloud Functions

Firebase CLI จะติดตั้ง Firebase Admin SDK และ Firebase SDK สำหรับโมดูลฟังก์ชันระบบคลาวด์โดยอัตโนมัติเมื่อคุณเริ่มต้นโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับการเพิ่มไลบรารีของบุคคลที่สามลงในโปรเจ็กต์ได้ที่ทรัพยากร Dependency ของแฮนเดิล

เพิ่มฟังก์ชัน "เพิ่มข้อความ"

สำหรับฟังก์ชัน "เพิ่มข้อความ" ให้เพิ่มบรรทัดต่อไปนี้ลงในไฟล์ต้นฉบับ

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.")

ฟังก์ชัน "เพิ่มข้อความ" เป็นปลายทาง HTTP คำขอที่ส่งไปยังปลายทางจะส่งผลให้เกิดออบเจ็กต์คำขอและการตอบกลับที่ส่งไปยังเครื่องจัดการคำขอสำหรับแพลตฟอร์มของคุณ (onRequest() หรือ on_request)

ฟังก์ชัน HTTP เป็นแบบซิงโครนัส (คล้ายกับฟังก์ชันที่เรียกใช้ได้) คุณจึงควรส่งการตอบกลับโดยเร็วที่สุดและเลื่อนงานออกไปโดยใช้ Cloud Firestore ฟังก์ชัน HTTP "เพิ่มข้อความ" จะส่งค่าข้อความไปยังปลายทาง 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} ล้อมรอบ "พารามิเตอร์" ซึ่งเป็นไวลด์การ์ดที่แสดงข้อมูลที่ตรงกันในโค้ดเรียกกลับ Cloud Firestore จะทริกเกอร์การเรียกกลับเมื่อมีการเพิ่มข้อความใหม่

ใน Node.js ฟังก์ชันที่ขับเคลื่อนด้วยเหตุการณ์ เช่น เหตุการณ์ Cloud Firestore จะเป็นแบบไม่พร้อมกัน ฟังก์ชันเรียกกลับควรแสดงผล null, ออบเจ็กต์ หรือ Promise หากคุณไม่แสดงผลใดๆ ฟังก์ชันจะหมดเวลา ส่งสัญญาณข้อผิดพลาด และจะพยายามใหม่ โปรดดูหัวข้อซิงค์ ไม่พร้อมกัน และสัญญา

จำลองการดำเนินการของฟังก์ชัน

ชุดโปรแกรมจำลองภายในของ Firebase ช่วยให้คุณสร้างและทดสอบแอปในเครื่องได้แทนที่จะนำไปใช้ในโปรเจ็กต์ Firebase ขอแนะนําอย่างยิ่งให้ใช้การทดสอบในเครื่องระหว่างการพัฒนา ซึ่งส่วนหนึ่งเป็นเพราะลดความเสี่ยงจากข้อผิดพลาดในการเขียนโค้ดซึ่งอาจเกิดค่าใช้จ่ายในสภาพแวดล้อมเวอร์ชันที่ใช้งานจริง (เช่น การวนซ้ำที่ไม่มีที่สิ้นสุด)

วิธีจำลองฟังก์ชัน

  1. เรียกใช้ firebase emulators:start และตรวจสอบเอาต์พุตสำหรับ URL ของ UI ชุดโปรแกรมจำลอง ซึ่งมีค่าเริ่มต้นเป็น localhost:4000 แต่อาจโฮสต์อยู่บนพอร์ตอื่นในเครื่อง ป้อน URL ดังกล่าวในเบราว์เซอร์เพื่อเปิด UI ชุดโปรแกรมจำลอง

  2. ตรวจสอบเอาต์พุตของคำสั่ง firebase emulators:start สำหรับ URL ของฟังก์ชัน HTTP โดยจะมีลักษณะคล้ายกับ http://localhost:5001/MY_PROJECT/us-central1/addMessage ยกเว้นดังนี้

    1. ระบบจะแทนที่ MY_PROJECT ด้วยรหัสโปรเจ็กต์
    2. พอร์ตอาจแตกต่างออกไปในเครื่องของคุณ
  3. เพิ่มสตริงการค้นหา ?text=uppercaseme ต่อท้าย URL ของฟังก์ชัน ซึ่งควรมีลักษณะดังนี้ http://localhost:5001/MY_PROJECT/us-central1/addMessage?text=uppercaseme นอกจากนี้ คุณยังเปลี่ยนข้อความ "ตัวพิมพ์ใหญ่" เป็นข้อความที่กำหนดเองได้

  4. สร้างข้อความใหม่ด้วยการเปิด URL ในแท็บใหม่ในเบราว์เซอร์

  5. ดูผลกระทบของฟังก์ชันต่างๆ ใน UI ชุดโปรแกรมจำลอง ดังนี้

    1. ในแท็บบันทึก คุณจะเห็นบันทึกใหม่ที่ระบุว่าฟังก์ชัน HTTP ทำงานสำเร็จแล้ว

      i functions: Beginning execution of "addMessage"

      i functions: Beginning execution of "makeUppercase"

    2. ในแท็บ Firestore คุณควรเห็นเอกสารที่มีข้อความต้นฉบับและข้อความเวอร์ชันตัวพิมพ์ใหญ่ (หากแต่เดิมเป็น "ตัวพิมพ์ใหญ่" คุณจะเห็น "ตัวพิมพ์ใหญ่")

ทำให้ฟังก์ชันใช้งานได้ในสภาพแวดล้อมเวอร์ชันที่ใช้งานจริง

เมื่อฟังก์ชันทำงานได้ตามที่ต้องการในโปรแกรมจำลองแล้ว คุณจะทำให้ใช้งานได้ ทดสอบ และเรียกใช้ฟังก์ชันในสภาพแวดล้อมการใช้งานจริงได้ โปรดทราบว่าหากต้องการทำให้ใช้งานได้ในเวอร์ชันที่ใช้งานจริง โปรเจ็กต์ต้องใช้แผนการตั้งราคาของ Blaze โปรดดูราคาของ Cloud Functions

หากต้องการจบบทแนะนำ ให้นำฟังก์ชันไปใช้แล้วเรียกใช้

  1. เรียกใช้คำสั่งนี้เพื่อทำให้ฟังก์ชันใช้งานได้:

     firebase deploy --only functions
     

    หลังจากเรียกใช้คำสั่งนี้แล้ว Firebase CLI จะแสดงผล URL สำหรับปลายทางของฟังก์ชัน HTTP ในเทอร์มินัล คุณจะเห็นบรรทัดดังต่อไปนี้

    Function URL (addMessage): https://us-central1-MY_PROJECT.cloudfunctions.net/addMessage
    

    URL มีรหัสโปรเจ็กต์และภูมิภาคสำหรับฟังก์ชัน HTTP แม้ว่าคุณจะไม่จําเป็นต้องกังวลใจในตอนนี้ แต่ฟังก์ชัน HTTP ที่ใช้งานจริงบางรายการควรระบุตําแหน่งเพื่อลดเวลาในการตอบสนองของเครือข่าย

    หากพบข้อผิดพลาดในการเข้าถึง เช่น "ให้สิทธิ์เข้าถึงโปรเจ็กต์ไม่ได้" ให้ลองตรวจสอบชื่อแทนโปรเจ็กต์

  2. ใช้เอาต์พุต URL โดย CLI เพิ่มพารามิเตอร์การค้นหาข้อความ แล้วเปิดในเบราว์เซอร์

    https://us-central1-MY_PROJECT.cloudfunctions.net/addMessage?text=uppercasemetoo
    

    ฟังก์ชันจะทำงานและเปลี่ยนเส้นทางเบราว์เซอร์ไปยังคอนโซล Firebase ที่ตำแหน่งฐานข้อมูลที่เก็บสตริงข้อความ เหตุการณ์การเขียนนี้จะทริกเกอร์ฟังก์ชัน "ทำให้ตัวพิมพ์ใหญ่" ซึ่งจะเขียนสตริงเวอร์ชันตัวพิมพ์ใหญ่

หลังจากทำให้ฟังก์ชันใช้งานได้และทำให้ใช้งานได้แล้ว คุณจะดูบันทึกได้ในคอนโซล Google Cloud หากจำเป็นต้องลบฟังก์ชันที่อยู่ระหว่างการพัฒนาหรือเวอร์ชันที่ใช้งานจริง ให้ใช้ Firebase CLI

ในเวอร์ชันที่ใช้งานจริง คุณอาจต้องการเพิ่มประสิทธิภาพการทำงานของฟังก์ชันและควบคุมค่าใช้จ่ายโดยการตั้งค่าจำนวนอินสแตนซ์ต่ำสุดและสูงสุดที่จะเรียกใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวเลือกรันไทม์เหล่านี้ได้ในส่วนควบคุมลักษณะการปรับขนาด

ขั้นตอนถัดไป

ในเอกสารประกอบนี้ คุณดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีจัดการฟังก์ชันสำหรับ Cloud Functions รวมถึงวิธีจัดการประเภทเหตุการณ์ทั้งหมดที่ Cloud Functions รองรับได้

หากต้องการดูข้อมูลเพิ่มเติมเกี่ยวกับ Cloud Functions ให้ทำดังนี้