以与客户端 SDK 最相似的方式使用管理 SDK 和可调用云功能删除一批文档?
Delete a batch of docs using admin SDK and callable cloud function in most similar way to client SDK?
我有一个文档 ID 数组,我想使用云函数删除它们,我的代码如下所示:
//If the user decieds on deleting his account forever we need to make sure we wont have any thing left inside of db after this !!
// incoming payload array of 3 docs
data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']}
export const deleteClients = functions.https.onCall(async (data, context) => {
try {
// declare batch
const batch = db.batch();
// set
data.arr.forEach((doc: string) => {
batch.delete(db.collection('Data'), doc);
});
// commit
await batch.commit();
} catch (e) {
console.log(e);
}
return null;
});
我在 batch.delete 如何将正确的参数传递给批量删除以引用我想在提交前提交删除的文档时遇到语法错误?
Delete 接受一个参数,即要删除的文档的文档引用。
data.arr.forEach((docId: string) => {
batch.delete(doc(db, "Data", docId));
});
您的代码中有几个错误:
data.arr.forEach()
无法工作,因为您的数据对象包含一个具有键 array
而不是键 arr
. 的元素
- 您混淆了 JS SDK v9 和 Admin SDK. See the write batch Admin SDK syntax here 的语法。
- 您需要在所有异步工作完成后向客户端发送回一些数据,以correctly terminate your CF。
- 您在 try/catch 块之后执行
return null;
:这意味着,在大多数情况下,您的 Cloud Function 将在异步工作完成之前终止(请参阅 link以上)
所以下面应该可以解决问题(未经测试):
const db = admin.firestore();
const data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']};
export const deleteClients = functions.https.onCall(async (data, context) => {
try {
const batch = db.batch();
const parentCollection = db.collection('Data')
data.array.forEach((docId) => {
batch.delete(parentCollection.doc(docId));
});
// commit
await batch.commit();
return {result: 'success'} // IMPORTANT, see explanations above
} catch (e) {
console.log(e);
// IMPORTANT See https://firebase.google.com/docs/functions/callable#handle_errors
}
});
我有一个文档 ID 数组,我想使用云函数删除它们,我的代码如下所示:
//If the user decieds on deleting his account forever we need to make sure we wont have any thing left inside of db after this !!
// incoming payload array of 3 docs
data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']}
export const deleteClients = functions.https.onCall(async (data, context) => {
try {
// declare batch
const batch = db.batch();
// set
data.arr.forEach((doc: string) => {
batch.delete(db.collection('Data'), doc);
});
// commit
await batch.commit();
} catch (e) {
console.log(e);
}
return null;
});
Delete 接受一个参数,即要删除的文档的文档引用。
data.arr.forEach((docId: string) => {
batch.delete(doc(db, "Data", docId));
});
您的代码中有几个错误:
data.arr.forEach()
无法工作,因为您的数据对象包含一个具有键array
而不是键arr
. 的元素
- 您混淆了 JS SDK v9 和 Admin SDK. See the write batch Admin SDK syntax here 的语法。
- 您需要在所有异步工作完成后向客户端发送回一些数据,以correctly terminate your CF。
- 您在 try/catch 块之后执行
return null;
:这意味着,在大多数情况下,您的 Cloud Function 将在异步工作完成之前终止(请参阅 link以上)
所以下面应该可以解决问题(未经测试):
const db = admin.firestore();
const data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']};
export const deleteClients = functions.https.onCall(async (data, context) => {
try {
const batch = db.batch();
const parentCollection = db.collection('Data')
data.array.forEach((docId) => {
batch.delete(parentCollection.doc(docId));
});
// commit
await batch.commit();
return {result: 'success'} // IMPORTANT, see explanations above
} catch (e) {
console.log(e);
// IMPORTANT See https://firebase.google.com/docs/functions/callable#handle_errors
}
});