无法读取云功能中的托管文件

Tho*_*hoe 2 javascript node.js firebase firebase-hosting google-cloud-functions

我的 Firebase 功能无法正常工作。这是日志:

在此处输入图片说明

11:01:03.932 AM warning getWatsonToken Error: ENOENT: no such file or directory, open '../public/javascript/services/watsonTokenValue.js'
    at Error (native)

11:01:03.831 AM warning getWatsonToken Uncaught exception

11:01:03.233 AM info    getWatsonToken 5YGY3R%2FBP0zelDOaob9PnxMWDj...

11:01:00.139 AM outlined_flag getWatsonToken Function execution took 804 ms, finished with status: 'ok'

11:00:59.834 AM info    getWatsonToken Executing function!

11:00:59.335 AM outlined_flag getWatsonToken Function execution started
Run Code Online (Sandbox Code Playgroud)

我不知道“未捕获的异常”指的是什么。

“ENOENT:没有这样的文件或目录”错误可能是节点路径错误。这是我的目录结构:

?? functions
    ??? index.js // this is my function
?? public
    ??? javascript
    ?   ??? services
    ?   ?   ??? watsonTokenValue.js // this is the file I want to write to
Run Code Online (Sandbox Code Playgroud)

这是显然导致错误的行:

fs.writeFile('../public/javascript/services/watsonTokenValue.js', tokenService, (err) => {
Run Code Online (Sandbox Code Playgroud)

该行使用 Node 写入文件。路径有问题吗?

这是完整的功能:

// Node modules
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const request = require('request'); // node module to send HTTP requests
const fs = require('fs');

admin.initializeApp(functions.config().firebase);

exports.getWatsonToken = functions.database.ref('userLoginEvent').onUpdate(event => { // authentication trigger when user logs in
  console.log("Executing function!");
  var username = '56ae6a1e7854',
  password = 'swordfish',
  url = 'https://' + username + ':' + password + '@stream.watsonplatform.net/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api';

  request({url: url}, function (error, response, body) {
    console.log(body);
    var tokenService = "app.value('watsonToken','" + body + "');";

    fs.writeFile('../public/javascript/services/watsonTokenValue.js', tokenService, (err) => {
      if (err) throw err;
      console.log('The file has been saved!');
    }); // close fs.writeFile

  }); // close request
return 0; // prevents an error message "Function returned undefined, expected Promise or value"
}); // close getWatsonToken
Run Code Online (Sandbox Code Playgroud)

前四行加载 Node 模块。该函数称为“getWatsonToken”。它在用户登录时触发,或者具体来说,当登录过程中的一行代码将用户的 Auth ID 写入 Firebase 实时数据库中的某个位置时,然后 onUpdate 触发该函数。接下来,为 HTTP 请求定义参数,然后将 HTTP 请求发送到 IBM Watson。IBM 为其语音转文本服务返回一个令牌作为 HTTP 响应的正文。(console.log 显示令牌,它正在工作。)然后一些 Angular JavaScript 被包裹在令牌周围。最后,文件被写入目录中的某个位置。最后一步似乎是错误所在。

这是我的目录结构的另一个视图:

在此处输入图片说明

谢谢你的帮助!

Dou*_*son 5

您不能使用 Cloud Function 读取和写入 Firebase 托管服务的文件,即使在同一个项目中也是如此。

当您使用 Firebase CLI 进行部署时,每个产品都是单独部署的。因此,您public文件夹中Firebase 托管的所有静态内容都会发送到一个地方,而您的所有 Cloud Functions 代码都会发送到另一个地方。在那之后,他们没有任何共同点。

可以阅读下的文件functions在部署后的文件夹。但显然,这些与部署后的托管无关。

我强烈建议不要尝试在 Cloud Function 中编写文件,尤其是对于您的用例。通过将持久数据写入实时数据库、Firestore 甚至云存储,可以更好地为您提供服务。Cloud Functions 中的磁盘文件不是永久性的,您可能有多个服务器实例运行彼此一无所知的函数。

  • @DougStevenson 如何读取函数文件夹中的文件。我试过使用 fs 但它不起作用? (2认同)