I’m trying to schedule a job when the user set the account to delete. Here’s the appraoch:
const accountDeletionTrigger = functions.firestore
.document('users/{userId}')
.onUpdate(async (change, context) => {
try {
const userId = context.params.userId;
const newValue = change.after.data();
const previousValue = change.before.data();
if (newValue.deleted && !previousValue.deleted) {
const deletionTime = new Date(Date.now() + 15 * 1000);
const task = await admin.scheduler.createJob({
name: `projects/${projectId}/locations/${location}/jobs/${jobId}`,
schedule: deletionTime.toISOString(),
timeZone: 'utc',
httpTarget: {
uri: `${baseUrl}/v1/deleteAccount`,
httpMethod: 'POST',
body: JSON.stringify({ userId }),
}
});
await admin.firestore().doc(`users/${userId}`).update({
deletionTaskId: task.name,
accountSetToDeleteAt: deletionTime,
});
}
} catch (error) {
console.error('Account deletion scheduling failed:', error);
}
});
But unfortunately, the logs on the cloud function says something like this:
{
"textPayload": "Account deletion scheduling failed: TypeError: Cannot read properties of undefined (reading 'createJob')",
"insertId": "676bc1560007e84741c1405d",
"resource": {
"type": "cloud_function",
"labels": {
"function_name": "accountDeletionTrigger",
"project_id": "devxi-dev",
"region": "us-central1"
}
},
"timestamp": "2024-12-25T08:24:54.518215Z",
"labels": {
"instance_id": "0066d924809867ef353866ac8a9aea4afab12f1e2ae52e5c0532bd079072058049ac376d12c934c340046d53fa8737784db52a56e248fea9c96fe14189acd492949733fb44e766f1696b88420229",
"execution_id": "j5w1h0iarwjy",
"runtime_version": "nodejs18_20241215_18_20_5_RC00"
},
"logName": "projects/devxi-dev/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
"trace": "projects/devxi-dev/traces/4dc6f1ed7b9b0ca1e67d16f0dfe0c9b1",
"receiveTimestamp": "2024-12-25T08:24:54.852172639Z"
}
Is there something I’m missing on? createJob
doesn’t exists for Firebase admin, then what’s the new way/alternative for this?
I’ve tried exploring the docs but couldn’t find something. I do come to know about pub/sub
triggers, but those are for calling a function in recursive way, I don’t want that.
Any kind of help would be appreciated. Thanks