skip to Main Content

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


  1. you are trying to use the function inside it self and that’s not correct

    Login or Signup to reply.
  2. I tried reproducing the error with minimal code – the following snippet will fail with a reference error.

    const obj = {
        deleteTranslations: (steps) => {
            console.log(steps);
            if (steps-1 >= 0) {
                deleteTranslations(steps-1);
            }
        }
    };
    
    obj.deleteTranslations(5)

    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:

    const obj = {
      someOtherProperty: 5,
      someFnProperty: function() {
        console.log(this);
        console.log(this.someOtherProperty);
      },
    };
    
    obj.someFnProperty()

    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 through this 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 how this 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:

    const obj = {
        deleteTranslations: function(steps) {
            console.log(steps);
            if (steps-1 >= 0) {
                this.deleteTranslations(steps-1);
            }
        }
    };
    
    obj.deleteTranslations(5);
    Login or Signup to reply.
  3. 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:

    1. Get Project and all project translations
    2. Create a params for DynamoDB Transaction
    • Add project key
    • Add list of translations
    1. Execute transaction (which deletes all of the items, if one fail, they all fail)

    https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/classes/transactwriteitemscommand.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search