transifex / transifex-javascript

Transifex Native SDK for Javascript
Apache License 2.0
43 stars 14 forks source link

[Bug] Saving translation throws `TypeError: Cannot read property 'id' of null` if reviewer is `null` #144

Closed psmyrek closed 2 years ago

psmyrek commented 2 years ago

Current Behavior I'm using @transifex/api v4.2.1 and I wanted to update one translation in my project by using this package, as described in the docs: https://github.com/transifex/transifex-javascript/tree/master/packages/api#changing-attributes. The translation is updated on Transifex side, but the .save() method throws an error: TypeError: Cannot read property 'id' of null.

Some debug notes that might help you fixing this issue

The _postSave() method iterates over all entries and it tries to read the id, but the id for the reviewer (which is null in my case) is not accessible. This is why it throws the TypeError: Cannot read property 'id' of null error.

obraz

Expected Behavior Updating the translation by using the .save() method should not throw an error, if nullable relationship is null.

Steps to Reproduce Call the translation.save() method on a translation, that does not have a reviewer set (reviewer is null);

Possible Solution Check the nullable relationships.

kostasgeo commented 2 years ago

@psmyrek The latest release fixes the issue.

Thank you for you feedback and the detailed steps.

bhousel commented 4 months ago

I noticed that with the latest version of @transifex/api (v7.1.1) I'm still able to trigger this error by saving the translation for a new ResourceTranslation. In my situation the error was that the translator was null.

I followed the example here: https://github.com/transifex/transifex-javascript/tree/master/packages/api#changing-attributes

Screenshot 2024-06-26 at 3 38 36 PM

My code looks something like below.
I get the crash in _postSave when it attempts to refresh the relations. I'm able to work around the issue by assigning a dummy user to the 'translator' relationship.

// Our project doesn't have this language yet, create one
if (!supported.has(languageID)) {
  await project.add('languages', [languages.get(languageID)]);
  // After adding the language, we should get a collection of empty ResourceTranslations below.
}

// Get the ResourceTranslations (for new language, they are empty slots)
const results = [];
const iter = api.ResourceTranslation.filter({ resource: resourceID, language: languageID });
for await (const val of iter.all()) {
  results.push(val);
}

// Try to update one.
const translation = results[0]
// weird: it will crash in `_postSave` looking for the `id` if we don't supply a translator here!
translation.set('translator', user);
translation.set('strings', { 'other': 'new string' });
await translation.save(['strings']);