我正试图在firebase云函数中使用从firebase工具中递归删除。我正在用模拟器测试这个。
但目前,我不是很成功。
CLI似乎正在使用Firestore REST API。它可以与模拟器一起使用吗
我的功能是:
import * as firebaseTools from 'firebase-tools';
import { db } from './admin';
const DEBUG = true;
export async function deleteUserData(userId) {
if (DEBUG) console.log('Delete user data', userId);
await firebaseTools.firestore.delete(`users/${userId}/contacts/`, {
project: db._projectId,
recursive: true,
yes: true, // auto-confirmation
});
if (DEBUG) console.log('User data deleted', userId);
}
这是来自模拟器的日志:
i functions: Beginning execution of "deleteUserData"
> Delete user data 4AiyOyCnAPSrKhc1Ycf6nVDqLoD2
> i You have set FIRESTORE_EMULATOR_HOST=tornado.local:3344, this command will execute against the firestore emulator running at that address.
⚠ Google API requested!
- URL: "https://cloudresourcemanager.googleapis.com/v1/projects/myapp-dev-f7v4:testIamPermissions"
- Be careful, this may be a production service.
⚠ External network resource requested!
- URL: "http://tornado.local:3344/v1beta1/projects/myapp-dev-f7v4/databases/(default)/documents/users/4AiyOyCnAPSrKhc1Ycf6nVDqLoD2:runQuery"
- Be careful, this may be a production service.
> Error with Delete FirebaseError: Failed to delete documents FirebaseError: HTTP Error: 403,
> Null value error. for 'list' @ L11
> at Timeout.<anonymous> (/Users/pitouli/Documents/GIT/myapp-app/functions/node_modules/firebase-tools/lib/firestore/delete.js:251:28)
> at listOnTimeout (internal/timers.js:549:17)
> at processTimers (internal/timers.js:492:7) {
> name: 'FirebaseError',
> children: [],
> context: undefined,
> exit: 1,
> message: 'Failed to delete documents FirebaseError: HTTP Error: 403, n' +
> "Null value error. for 'list' @ L11",
> original: undefined,
> status: 500
> }
i functions: Finished "deleteUserData" in ~1s
谢谢你的帮助!
编辑1:REST API似乎应该与模拟器一起工作,因为这里给出了它的示例:https://firebase.google.com/docs/emulator-suite/connect_and_prototype#clear_your_database_between_tests
我注意到,在我的情况下,请求是在v1beta1
端点上发出的,而在doc示例中是在v1
端点上发出。
编辑2:根据@sam的建议,我用非限制性规则进行了测试,它有效。但据我所知,云功能应该忽略规则(#gangsta(
这是我的";正常的";规则:
service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
}
match /users/{userId}/{document=**} {
allow create, read, update, delete: if request.auth.uid == userId;
}
}
}
下面是我用来测试的:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
这个错误已经被这个PR中的firebase团队修复。