NodeJS:firebase-tools firestore 删除失败

NodeJS: firebase-tools firestore delete failing

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

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

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环境变量

调试输出:

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=***.

设置它

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

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