I’m currently trying to implement functionality that deletes all translations within a project when a user deletes a project. However, when I try to call my deleteTranslations
function within my deleteProject
function, it says deleteTranslations
is not defined.
deleteTranslations: async({
user,
projectId,
trkeys
}) => {
const deletePromises = trkeys.map(async(trkey) => {
const params = {
...getBaseParams(),
Key: {
PK: getPK('trans', user, projectId),
SK: getSK('trans', user, projectId, trkey),
},
};
return doDelete(params);
});
return Promise.all(deletePromises);
},
deleteProject: async({
user,
project
}) => {
const translationsParams = {
user,
projectId: project,
trkeys: [],
};
await deleteTranslations(translationsParams); // the problem is here
const params = {
TableName: table,
Key: {
PK: getPK('project', user),
SK: getSK('project', user, project),
},
};
return doDelete(params);
},
}
I’ve tried to implement this in a few ways but I cant wrap my head around this.
3
Answers
you are trying to use the function inside it self and that’s not correct
I tried reproducing the error with minimal code – the following snippet will fail with a reference error.
While it may not be the exact setup you’re using, it certainly portrays the error(s) you’re running into.
The first error would be trying to access a key within an object without referencing
this
. Whenever you want to access something from the current context,this
has to be prepended, like such:The above snippet will log the context the function is called within – which is
obj
– and list all of its properties. By accessing the context throughthis
we can also access other properties within the context.You may have noticed me using the
function
keyword, rather than using an arrow function. This is also due to context-reasons and the "second" error you’re running into. Here’s a great article on howthis
works when looking at "regular" functions vs. arrow functions.I’d consider refactoring your code to use stand-alone functions as this can be fixed but doesn’t look all too clean. Here’s a working version:
You should use DynamoDB Transactions for this, so long as there is not more than 100 translations. This allows you to delete all translations and project in a single ATOMIC operation. Psuedo would look like this:
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/classes/transactwriteitemscommand.html