我正在Cloud Firestore触发器上编写云功能。我想要的是当在某些uuid下添加文档时,它必须在2分钟后删除并将相同的数据分配给另一个文档。我在下面写了一些代码
exports.createdOpenOrder = functions.firestore.document('Some/{psId}/Open/{OrderId}').onCreate((snap, context) => {
// Get an object representing the document
console.log("Deleting function execution started:");
const newValue = snap.data();
var OrderId = context.params.OrderId;
var psId = context.params.psId;
setTimeout(delete_cur, 120000);
function delete_cur() {
var data = db.collection('Some').doc(psId).collection('Open').doc(OrderId).delete().then(function() {
console.log("Document successfully deleted!");
// calling another function to reassign
reassign(OrderId);
return;
}).catch(function(error) {
console.error("Error removing document: ", error);
return;
});
}
});
现在我的问题是setTimeout函数在2分钟后没有完全调用,数据没有删除。我的代码有什么问题吗?请让我知道如何在setTimeout上完美地编写代码。
要找到问题,请在logTimeout处理程序的内容之前放置日志,然后查看它们的内容。
目前,您只是在delete
异步函数返回后捕获异常。在调用delete
之前,链中的所有其他异常都没有被捕获。
function delete_cur() {
console.log('handler called')
try {
var data = db.collection('Some').doc(psId).collection('Open').doc(OrderId).delete().then(function() {
console.log("Document successfully deleted!");
// calling another function to reassign
reassign(OrderId);
return;
}).catch(function(error) {
console.error("Error removing document: ", error);
return;
});
} catch (e) {
console.error('could not invoke delete', e)
}
}