movement.entities.reduce is not a function or its return value is not iterable

84 Views Asked by At

So I have no idea what is going on, I got this code from a friend and I'm slowly learning how it works. This code is supposed to generate a random "movement" action inside a video game. It used to work smoothly, but after a couple of updates and features, this happens. So this is the error message:

TypeError [Error]: movement.entities.reduce is not a function or its return value is not iterable

This is the code:

export async function generateActions(prompt: string) {
    const { data, status } = await axios.get<WitResponse>(
        'https://api.wit.ai/message',
        {
            params: {
                q: prompt,
            },
            headers: {
                Authorization: `Bearer ${config.witai_key}`,
            },
        },
    );

    if (status !== 200) return [];

    const movements =
        data.entities['movement_with_repeat:movement_with_repeat'] ?? [];

    const instructions: MovementInstruction[] = [];


    for (const movement of movements) {
        const [action, count] = movement.entities.reduce(
            (a, b) => {
                if (b.confidence < 0.9 || b.value === 'spin') return a;
                if (b.name === 'wit$number') a[1] = b.value;
                else if (b.name === 'movement_type') a[0] = b.value;

                return a;
            },
            [null, 1] as [Direction | null, number],
        );

        if (action) {
            instructions.push({
                direction: action,
                distance: count,
            });
        }
    }

    return instructions;
}

This is a huge program with probably over 10.000 lines of code. So there are some functions and objects that aren't shown here.

0

There are 0 best solutions below