I want to use a combination of TTL and delete Triggers, to delete items in a second database.
Every item in my collection has a link to a corresponding image, that is stored in a vercel blob storage.
If the Item is deleted from my collection, I want to delete the corresponding image in the blob.
To do this, I set up a function in a delete trigger, that uses the image link, to delete the image.
Here is the code I used:
exports = async function(changeEvent) {
try {
// Import the Vercel Blob SDK
const { del } = require('@vercel/blob');
// Get the full document before it was deleted
const deletedDoc = changeEvent.fullDocumentBeforeChange;
if (!deletedDoc || !deletedDoc['flyerUrl']) {
console.log('No flyer image found in deleted document');
return;
}
const flyerUrl = deletedDoc['flyerUrl'];
console.log(`Attempting to delete flyer image: ${flyerUrl}`);
// Set up Vercel Blob configuration
// These values should be stored as secrets in your MongoDB Atlas environment
const config = {
token: context.values.get("VERCEL_BLOB_TOKEN")
};
// Delete the blob directly
await del(flyerUrl, config);
console.log(`Successfully deleted blob: ${flyerUrl}`);
} catch (error) {
console.error("Error in delete trigger function:", error.message);
}
The VERCEL_BLOB_TOKEN was saved as a secret for MongoDB to access.
Since this code requires the vercel/blob API, I used the “add Dependancy” function to include @vercel/blob
I was shown that the import was successful.
When running the function, I was prompted an error:
Error in delete trigger function: failed to compile source for module '@vercel/blob': SyntaxError: node_modules/@vercel/blob/dist/index.cjs: Line 35:7 Unexpected token function (and 30 more errors)
> result:
{
"$undefined": true
}
> result (JavaScript):
EJSON.parse('{"$undefined":true}')
From this point on, I am unsure what to do. Did the import not work correctly, or is this a problem with the @vercel/blob api?
Does anyone have experience with this and can point me in the right direction?