ค้นหาเว็บไซต์

typescript - ไม่สามารถเข้าถึงเมธอดระดับโลกในโปรเจกต์ Firebase Functions


ฉันคาดหวังว่าหลายฟังก์ชันในโปรเจกต์ Firebase admin ของฉันจะมีเมธอดเหมือนกัน เช่น "สร้างสตริงตัวอักษรและตัวเลขแบบสุ่ม" ดังนั้นเพื่อหลีกเลี่ยงการเขียนโค้ดซ้ำ ฉันจึงสร้างโฟลเดอร์ Utils และไฟล์ utilityFunctions.ts ภายในโฟลเดอร์นั้น ไฟล์นี้มีเพียงฟังก์ชัน export หนึ่งฟังก์ชันที่สร้างสตริงตัวอักษรและตัวเลข ฉันได้นำเข้าไฟล์นั้นในไฟล์ฟังก์ชันหนึ่งของฉันด้วยคำสั่ง import

นี่คือไฟล์ของฉันที่มีวิธีการระดับโลกที่ตั้งใจไว้: src/Utils/utilityFunctions.ts

export const theRandomDocId = function randomDocumentId(length: number): 
String {
// the code to generate a random string
}

นี่คือหนึ่งในไฟล์ฟังก์ชันของฉัน: src/Compliment/addNewCompliment.ts

import * as functions from 'firebase-functions'
const admin = require('firebase-admin')
//This import statement below for the utilityFunctions file
import utilityFunctions = require('../Utils/utilityFunctions')

export const addTheNewCompliment = functions.region('asia-east2').https.onCall((complimentData, 
context) => {

//generate random 11 alphanumeric ComplimentId converted to String
const randomComplimentId = utilityFunctions.theRandomDocId(28)

//There is a lot more code in this file but since its not relevant to the 
//problem, I have excluded it.

}

นี่คือข้อผิดพลาดที่แสดงโดย VS Code:

!  functions[addNewCompliment(asia-east2)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '../Utils/utilityFunctions'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/lib/Compliment/addNewCompliment.js:5:26)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)

ฉันยังได้ลองประกาศเมธอดโดยไม่ใช้ตัวแปร export และเข้าถึงเมธอดจากไฟล์ addNewCompliment.ts ของฉันโดยไม่ใช้คำสั่ง import โค้ดสามารถคอมไพล์ได้ แต่เมื่อมันถูกเรียกใช้ บันทึกของ Firebase Functions จะเกิดข้อผิดพลาด:

Unhandled error ReferenceError: randomDocumentId is not defined

นี่คือไฟล์ package.json ของฉัน

{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"main": "lib/index.js",
"dependencies": {
"@google-cloud/storage": "^4.7.0",
"@types/sharp": "^0.25.0",
"child-process-promise": "^2.2.1",
"firebase-admin": "^8.9.0",
"firebase-functions": "^3.6.2",
"fs-extra": "^9.0.0",
"sharp": "^0.25.2"
},
   "devDependencies": {
"tslint": "^5.12.0",
"typescript": "^3.2.2",
"firebase-functions-test": "^0.1.6"
},
"private": true
}

คำตอบ #1

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