Javascript examples

I’m in need of some custom code to chop up and manipulate an output, and I’m struggling to find examples of existing javascript code to help me.

The module comes with the following default code:

export const code = async (inputs) => {
  return true;
};

The template Write long SEO articles by ChatGPT on WordPress has code

export const code = async (params) => {
    return { ...JSON.parse(params.chatgpt), num: Math.floor(Math.random() * 100000) };
};

When I run the example, the code fails because the chatgpt module seems to be giving Json in a format it doesn’t accept.

I’m a programmer, but a javascript novice. Can anybody point me at any resources/examples for a flow with javascript that works?

Hi @Ivan,

Hard to say for sure without knowing what the inputs are or the error logs. Have you considered to use these pieces?

image

Kind regards,

I figured it out. The chatgpt output was wrapping the JSON in it’s own json delimiters. I had to modify the prompt to include

DO NOT put ‘’’ json at the start of the output or ‘’’ at the end of the output.

Posting, in case it helps anybody. Most likely I am being stupid and should have anticipated this.

1 Like

@Ivan,

Ahh yes, its preferred output format generally chooses Markdown over plain text. So it will use the markdown for a code block which of course is:

    ```some-language
        // some code
    ```

Glad you managed to resolve it.

In case you are unaware, you can use console.log() in your code and have it output the results. See my reply here for an example.

Kind regards,