NodeJS:firebase工具firestore删除失败



删除父文档时,我正试图删除子集合。

为此,我创建了以下云功能:

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

即使列表文档(动漫(已经被删除,这也能正常工作,所以我不明白为什么函数中的那个不起作用。

编辑:设置DEBUGenv 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=***设置它。

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

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

最新更新