Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 无法访问Firebase Functions项目中的全局方法_Typescript_Firebase_Google Cloud Functions - Fatal编程技术网

Typescript 无法访问Firebase Functions项目中的全局方法

Typescript 无法访问Firebase Functions项目中的全局方法,typescript,firebase,google-cloud-functions,Typescript,Firebase,Google Cloud Functions,我希望Firebase管理项目中的多个函数具有相同的方法,例如:“生成随机字母数字字符串”。因此,为了避免出现锅炉板代码,我在该文件夹中创建了一个Utils文件夹和一个utilityFunctions.ts文件。该文件只包含一个生成字母数字字符串的导出函数。我已经用import语句将该文件导入到我的一个函数文件中 下面是我的文件,其中包含预期的全局方法:src/Utils/utilityFunctions.ts export const theRandomDocId = function ran

我希望Firebase管理项目中的多个函数具有相同的方法,例如:“生成随机字母数字字符串”。因此,为了避免出现锅炉板代码,我在该文件夹中创建了一个Utils文件夹和一个utilityFunctions.ts文件。该文件只包含一个生成字母数字字符串的导出函数。我已经用import语句将该文件导入到我的一个函数文件中

下面是我的文件,其中包含预期的全局方法:src/Utils/utilityFunctions.ts

export const theRandomDocId = function randomDocumentId(length: number): 
String {
// the code to generate a random string
}
下面是我的一个函数文件:src/compaind/addnewcommplion.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代码显示的错误:

!  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)
这是我的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
}

您的
Utils
目录是否在您的
functions
目录中?部署时,只有
功能
目录被发送到服务器,因此不在该目录中的任何内容都不会被上传。如果这不是问题所在,请确保您的大小写匹配,因为云函数后端有一个区分大小写的文件系统。

Hi Michael,是的,它位于Functions目录中。关于大写,我不明白我到底做错了什么,上面提到的所有代码。。。有什么东西我遗漏了吗。。请您详细介绍一下……您是否记得将其作为部署包的一部分?如果您可以使用有关文件结构的更多信息以及package.json文件更新问题,这将非常有用。Hi pessolato,我已经使用package.json文件更新了问题,我没有将本地模块作为部署包的一部分。。。将其添加到package.json文件时,我得到以下错误:错误:经过大量阅读和反复尝试,找不到模块“../Utils/utilityFunctions”Hi pessolato。我知道了本地模块是如何工作的。。。。谢谢你的评论,它为我指明了正确的方向。。。。。
{
"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
}