【问题标题】:NodeJS: firebase-tools firestore delete failingNodeJS:firebase-tools firestore 删除失败
【发布时间】:2021-07-12 09:38:02
【问题描述】:

我正在尝试在删除父文档时删除子集合。

为此,我创建了以下云函数:

import { firestore, logger } from 'firebase-functions';
import { recursiveDelete } from '../utils/db';

export const deleteListItems = firestore.document('owners/{owner}/lists/{list}').onDelete(snap => {
  const collPath = snap.ref.collection('items').path;
  logger.info('Why do you hate me?');
  logger.debug({ collPath });
  return recursiveDelete(collPath);
});

---------
db.ts
---------------------------------
import * as functions from 'firebase-functions';
const firebase_tools = require('firebase-tools');

export const recursiveDelete = (path: string): Promise<any> => {
  console.debug({ project: process.env.GCLOUD_PROJECT });
  console.debug({ token: functions.config().fb.token });

  return firebase_tools.firestore.delete(path, {
    project: process.env.GCLOUD_PROJECT,
    recursive: true,
    yes: true,
    token: functions.config().fb.token,
  });
};

从我的日志中,我可以看到我要删除的路径是正确的,owners/G-470728052545159171/lists/anime/items。我还可以看到项目和令牌值存在。

但是,我在云功能日志中看到以下错误:

deleteListItems 
FirebaseError: Deletion failed. Errors: Failed to fetch documents to delete >= 3 times..
   at Timeout.<anonymous> (/workspace/node_modules/firebase-tools/lib/firestore/delete.js:272:32)
   at listOnTimeout (internal/timers.js:554:17)
   at processTimers (internal/timers.js:497:7) 

理想情况下,我希望在列表文档级别执行删除,但我认为,被删除的文档可能已经导致错误,显然不是。

我尝试在控制台中运行等效命令:

firebase firestore:delete -r -y owners/G-470728052545159171/lists/anime/items

即使列表文档(动漫)已被删除,这仍按预期工作,所以我不明白为什么函数中的那个不起作用。

编辑:设置DEBUG env var

调试输出:

2:05:30.175 PM
deleteListItems
[2021-04-17T18:05:30.171Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
2:05:30.176 PM
deleteListItems
[2021-04-17T18:05:30.175Z] > authorizing via --token option
2:05:30.176 PM
deleteListItems
[2021-04-17T18:05:30.176Z] [iam] checking project watchlist-bot for permissions ["datastore.entities.delete","datastore.entities.list","firebase.projects.get"] 
2:05:30.246 PM
deleteListItems
[2021-04-17T18:05:30.244Z] > refreshing access token with scopes: ["email","https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","openid"] 
2:05:30.246 PM
deleteListItems
[2021-04-17T18:05:30.245Z] >>> HTTP REQUEST POST https://www.googleapis.com/oauth2/v3/token   
2:05:30.246 PM
deleteListItems
 <request body omitted> 
2:05:30.478 PM
deleteListItems
[2021-04-17T18:05:30.477Z] <<< HTTP RESPONSE 400 {"cache-control":"no-cache, no-store, max-age=0, must-revalidate","date":"Sat, 17 Apr 2021 18:05:30 GMT","pragma":"no-cache","expires":"Mon, 01 Jan 1990 00:00:00 GMT","content-type":"application/json; charset=utf-8","vary":"X-Origin, Referer, Origin,Accept-Encoding","server":"scaffolding on HTTPServer2","x-xss-protection":"0","x-frame-options":"SAMEORIGIN","x-content-type-options":"nosniff","alt-svc":"h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"","accept-ranges":"none","transfer-encoding":"chunked"} 
2:05:30.581 PM
deleteListItems
[2021-04-17T18:05:30.581Z] <<< HTTP RESPONSE 401 {"www-authenticate":"Bearer realm=\"https://accounts.google.com/\", error=\"invalid_token\"","vary":"X-Origin, Referer, Origin,Accept-Encoding","content-type":"application/json; charset=UTF-8","date":"Sat, 17 Apr 2021 18:05:30 GMT","server":"ESF","cache-control":"private","x-xss-protection":"0","x-frame-options":"SAMEORIGIN","x-content-type-options":"nosniff","server-timing":"gfet4t7; dur=4","alt-svc":"h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"","accept-ranges":"none","transfer-encoding":"chunked"} 
2:05:30.581 PM
deleteListItems
[2021-04-17T18:05:30.581Z] <<< HTTP RESPONSE BODY {"error":{"code":401,"message":"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","status":"UNAUTHENTICATED"}} 
2:05:30.583 PM
deleteListItems
[2021-04-17T18:05:30.583Z] [iam] error while checking permissions, command may fail: FirebaseError: HTTP Error: 401, Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

我怀疑我的令牌似乎有问题,但我不知道为什么。我按照他们的规定使用firebase login:ci 生成了令牌。 然后我使用firebase functions:config:set fb.token=*** 设置它。

【问题讨论】:

  • 就在require('firebase-tools'); 之前,添加process.env.DEBUG = true。这应该会提供更多(可能太多)关于正在发生的事情的信息。文档声明某些功能需要文件系统访问权限,但我怀疑这是一个。
  • @samthecodingman 我已经包含了调试输出中有趣的部分。有什么想法吗?

标签: node.js firebase google-cloud-firestore google-cloud-functions firebase-tools


【解决方案1】:

正如您在日志消息中看到的,您当前由login:ci 生成的令牌错误地缺少"https://www.googleapis.com/auth/cloud-platform" 范围。尝试更新您用于创建令牌的 firebase-tools 实例并尝试生成新令牌。

关于为什么您的本地系统上没有出现同样的错误:在您的系统上,firebase-tools 使用您的完整帐户凭据(部署代码等)而不是具有目标范围的凭据。

【讨论】:

  • 这行得通,谢谢。但是,我想知道:这个令牌会过期吗?
  • @Ace 生成的令牌是 OAuth 2 刷新令牌。除非您撤销它 from your Google Account(显示为 Firebase CLI)或更改帐户密码,否则此令牌不会“过期”。但是,使用它生成的访问/ID 令牌将有 1 小时的有效期。
  • 明白。谢谢。
猜你喜欢
  • 2021-08-09
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 2023-03-12
  • 2018-04-12
  • 2019-10-19
  • 2021-05-16
  • 2012-08-14
相关资源
最近更新 更多