0

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.

  • What is the data in `movements` which causes an error? – VLAZ May 05 '22 at 12:04
  • This is the whole error `TypeError [Error]: movement.entities.reduce is not a function or its return value is not iterable at generateActions (C:\Users\Gauti\Desktop\thearchon-main\thearchon-main\dist\utils.js:82:51) at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:96:5) at async Promise.all (index 1) at async EventEmitter. (C:\Users\Gauti\Desktop\thearchon- main\thearchon-main\dist\classes\BaseBot.js:764:45)` – himmispiderman May 07 '22 at 09:47

0 Answers0