在 shopware Administration vie 模块中创建新产品时无法通过翻译

Can't pass translations when creating a new Product in shopware Administration vie Module

我尝试通过管理模块创建产品,到目前为止效果很好,但我无法通过其他语言的翻译。 我遵循了这个指南:https://shopware.stoplight.io/docs/admin-api/docs/guides/writing-entities/associations.md#translated-fields

这是我的代码:

createProduct() {
const getTaxId = () => {
    const criteria = new Criteria();
    this.taxRepository.create('tax');
    return this.taxRepository.search(criteria, Shopware.Context.api);
};

getTaxId().then(result => {
    const taxId = result[0].id;

    const newProduct = this.productRepository.create(Shopware.Context.api);

    const randomProductHash = Math.floor(Math.random() * 100000);
    newProduct.name = 'Das TestProdukt ' + randomProductHash;
    newProduct.taxId = taxId;

   // translation setted like in the guide

    newProduct.translations = {
        '2fbb5fe2e29a4d70aa5854ce7ce3e20b': {
            name: 'Das TestProdukt ' + randomProductHash,
            description: 'english description by code',
        },
        '2d7d7d698f634a04b5796e49aa846ae6': {
            name: 'The testProdckt ' + randomProductHash,
            description: 'german description by code',
        },
    };

    newProduct.price = [
        {
            currencyId: 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
            extensions: [],
            gross: (Math.random() * 500 + 500).toFixed(2),
            linked: true,
            listPrice: null,
            net: (Math.random() * 250 + 250).toFixed(2),
        },
        {
            currencyId: 'f772f2a317324871a9bb441c34ca0094',
            extensions: [],
            gross: (Math.random() * 500 + 500).toFixed(2),
            linked: true,
            listPrice: null,
            net: (Math.random() * 250 + 250).toFixed(2),
        },
    ];

    newProduct.productNumber = 'DEMO_' + Math.floor(Math.random() * 100000);
    newProduct.stock = 1;
    console.log(newProduct);
    const context = Shopware.Context.api;
    context.systemLanguageId = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
    this.productRepository.save(newProduct, context).then(e => {
        console.log(e);
    });
});

但是当我提交它时出现错误:

changeset-generator.data.js?13e4:177 -  Uncaught (in promise) TypeError: draft.forEach is not a function
  at ChangesetGenerator.handleOneToMany (changeset-generator.data.js?13e4:177)
    at eval (changeset-generator.data.js?13e4:98)
    at eval (entity-definition.data.js?1f64:119)
    at Array.forEach (<anonymous>)
    at EntityDefinition.forEachField (entity-definition.data.js?1f64:118)
    at ChangesetGenerator.recursion (changeset-generator.data.js?13e4:53)
    at ChangesetGenerator.generate (changeset-generator.data.js?13e4:34)
    at Repository.save (repository.data.js?1d07:103)
    at eval (index.js?cce3:158)

看起来 forEach 试图循环遍历“translations”对象,这当然不可能作为对象。那么翻译对象的结构必须如何?

好的我明白了,我不知道是文档有误还是我看错了space。但这就是它的工作原理:

您必须向其添加新的翻译,而不是设置 newProduct.translation 变量。

let translation = this.translationRepository.create(Shopware.Context.api);

translation.languageId = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
translation.name = 'THE';
newProduct.translations.add(translation);

translation = this.translationRepository.create(Shopware.Context.api);
translation.languageId = '2d7d7d698f634a04b5796e49aa846ae6';
translation.name = 'DAS';
newProduct.translations.add(translation);

此外,您必须将此添加到计算方法中:

translationRepository() {
    return this.repositoryFactory.create('product_translation');
},