我的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我不知道“秘密例外”指的是什么。
"ENOENT:没有这样的文件或目录“错误可能是节点路径错误。以下是我的目录结构:
── functions
├── index.js // this is my function
── public
├── javascript
│ ├── services
│ │ ├── watsonTokenValue.js // this is the file I want to write to这条线显然是导致错误的原因:
fs.writeFile('../public/javascript/services/watsonTokenValue.js', tokenService, (err) => {该行使用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前四行加载节点模块。这个函数叫做“getWatsonToken”。当用户登录时,或者具体地说,当登录进程中的一行代码将用户的Auth ID写入Firebase实时数据库中的某个位置时,它将触发,然后onUpdate触发该函数。接下来,为HTTP请求定义参数,然后将HTTP请求发送给IBM。IBM返回其语音到文本服务的令牌作为HTTP响应的主体。( console.log显示令牌,这是有效的。)然后,一些角度的JavaScript被包裹在令牌周围。最后,将文件写入目录中的某个位置。最后一步似乎是错误所在。
下面是我的目录结构的另一个视图:

谢谢你的帮忙!
发布于 2018-01-23 18:39:10
即使在同一个项目中,您也不能使用云函数来读取和写入Firebase宿主提供的文件。
当您使用Firebase CLI进行部署时,每个产品都分别部署。因此,将public文件夹中的Firebase托管的所有静态内容发送到一个地方,将所有云函数代码发送到另一个地方。在那之后,他们没有任何共同之处。
部署后,可以读取functions文件夹下的文件。但很明显,这些都与部署后的主机无关。
我强烈建议不要尝试在云函数中编写文件,特别是在用例中。您想要做的更好的方法是将持久数据写入Realtime、Firestore甚至Cloud。云函数中的磁盘文件不是持久化的,您可能有多个服务器实例,这些服务器运行的函数彼此一无所知。
发布于 2018-01-23 23:18:44
谢谢道格!下面是我的工作Firebase函数:
// 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
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) {
admin.database().ref('IBMWatsonToken').set({'token': body})
.then(function() {
console.log("Token saved!");
}); // close promise
}); // close request
return 0; // prevents an error message "Function returned undefined, expected Promise or value"
}); // close getWatsonToken这会将令牌写入我的Firebase实时数据库。我的控制器代码是:
firebase.database().ref('IBMWatsonToken').on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
$scope.watsonToken = childSnapshot.val();
});
});此代码引用Firebase实时数据库中的位置,然后on侦听值中的更改,然后forEach读取值并将值放在$scope上以供控制器函数使用。
我将通过删除函数中的诺言和console.log来使其更快。
https://stackoverflow.com/questions/48408563
复制相似问题