P
P
picka2021-02-25 22:16:01
JavaScript
picka, 2021-02-25 22:16:01

How to correctly pass the received data from Computed to Vue?

There is a function in Computed:

SendData() {
            let FormData = this.AddProduct

                let Inputs = FormData.Inputs.reduce( (acc, cur ) => {
                    acc[cur.Alias] = cur.Data
                    return acc
                }, {})

                let Quill = FormData.Quill.reduce( (acc, cur ) => {
                    acc[cur.Alias] = cur.Data
                    return acc
                }, {})

            let OptionsData = this.Options

                let OptionsObjects = OptionsData.reduce( (acc, cur ) => {
                    acc[cur.AliasSingle] = cur.Data
                    return acc
                }, {})

            let Objects = Object.assign(Inputs, Quill)

                Objects.Options = OptionsObjects
                
                // Генерируем все возможные вариации продукта

                    const OptionsKeys = Object.keys(OptionsObjects)
                    const OptionsValues = Object.values(OptionsObjects)

                    const OptionsAliasSets = getCartesianProduct(
                        OptionsValues.map(options => options.map(({ Alias }) => Alias))
                    );
                    const OptionsNameSets = getCartesianProduct(
                        OptionsValues.map(options => options.map(({ Name }) => Name))
                    );

                    const VariationsArray = OptionsAliasSets.map(
                        (set, index) => Object.fromEntries([
                            ['VendorCode', `${Objects.VendorCode}.${set.join('.')}`],
                            ...OptionsKeys.map((name, i) => [name, OptionsNameSets[index][i]]),
                        ])
                    );

                    function getCartesianProduct(sets) {
                        const result = []

                        function cartesianProduct(sets, index, current) {
                            if (index === sets.length) {
                                result.push(current.slice());
                                return;
                            }
                            for (let i = 0; i < sets[index].length; i++) {
                                current[index] = sets[index][i];
                                cartesianProduct(sets, index + 1, current);
                            }
                        }

                        cartesianProduct(sets, 0, []);
                        return result;
                    }
                    
                    // Добавляем все вариации продукта в общий объект
                    Objects.Variations = VariationsArray

            return Objects
        }
    },

It gets other data from the store, pulled in by other Computed functions.
I'm trying to pass the received data from SendData to the component via Props, but I get the following error
Cannot read property 'Inputs' of undefined
In this snippet
let Inputs = FormData.Inputs.reduce( (acc, cur ) => {
                    acc[cur.Alias] = cur.Data
                    return acc
                }, {})

What are the ways to solve this problem? And how can it be thrown not through Props, but let's throw it into Vuex

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim, 2021-02-25
@picka

Well, then you don't have the Inputs property in FormData.

let FormData = this.AddProduct  // --> this.AddProducts не содержит свойства Inputs

Moreover, it looks like FormData is undefined.
That is this.AddProducts- undefined

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question