[英]`firebase_tools.firestore .delete` requires a token, but where does that token come from?
我有一个 Firebase 云(可调用)function,它删除了一个 firestore 文档和所有子实体,包括子集合。 我通过稍微修改 function 在他们的文档中提供我的 firebase 来做到这一点: https://firebase.google.com/docs/firestore/solutions/delete-collections
重要的一点在这里:
...
return firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
...
当我从我的网页调用这个 function 时(在用户验证后),web 客户端抛出一个错误,远程 function 导致错误。 在 firebase 控制台,我发现这个错误:
Unhandled error TypeError: Cannot read property 'token' of undefined
这指向上面代码片段中的行: token: functions.config().fb.token
。 所以.fb
是 null。
这里发生了什么?
搜索 web 告诉我一些关于login:ci
在命令行的信息,但是虽然我在我的笔记本电脑上开发它,但是当部署应用程序时,将没有命令行。 该网站将在 firebase 托管。 它调用 firebase 云 function。我正在使用 firebase 身份验证进行用户身份验证(电子邮件/密码)并将数据存储在 firestore 中。
此外,我已经在命令行上启用了,因为我可以执行firebase deploy --only functions
正常。 如何确保functions.config().fb
不返回 null?
如果需要,您可以直接将令牌字符串粘贴到代码中,替换functions.config().fb.token
。 该页面的作者可能认为您不想这样做,因此他们建议您应该为它创建一个配置项。
如果您不想粘贴它,而您实际上想配置 function 以从配置中获取该值,则必须使用functions:config:set在命令行上设置配置,如下所示:
firebase functions:config:set fb.token=<YOUR-TOKEN>
再次部署您的 function,以便它可以使用该值。
如果您发现文档令人困惑,我建议您使用页面右上角的“发送反馈”链接。
该令牌是一个登录令牌,用于登录您在服务器上的帐户以获得执行命令的权限,就像您使用 Firebase CLI 一样。 您只需要生成一次并部署它。
从firebase-functions v3.18.0
,建议使用 dotenv 格式来存储变量。 https://firebase.google.com/docs/functions/config-env#env-variables
firebase login:ci
functions/
中创建一个.env
文件TOKEN=<your_token_from_step_1>
await firebase_tools.firestore.delete(path, { project: process.env.GCLOUD_PROJECT, recursive: true, yes: true, force: true token: process.env.TOKEN, // Like this });
firebase deploy --only functions
注意:我添加了force: true
因为这个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.