Passing array to function

Hello,

If I am doing something as below:

const mostPopularVids = async () =>
await (await fetch(‘/.netlify/functions/mostpopular’)).json();

How do I pass an array to a function via javascript? I.E. ‘var ids = [ ];’

I understand how do it via a HTML form but cannot find info on doing it via just js.

Hi there,
the person who knows most about functions is out today, but here is an idea:

did you see this functions playground already? there might be an example of what you are trying to do in here.

We can’t, as a general rule, help you learn, write, or debug code (there are sadly only 8 support engineers for hundreds of thousands of sites worldwide) BUT there are many knowledgeable people here in these forums who might have answers for you if you can narrow it down a bit.

Hi jbeat497 :wave:t2:

The question you’re asking is one more of learning the basics of JavaScript and how to fetch than one specifically oriented at how to use Netlify or its tools. That’s why @perry has to say

We can’t, as a general rule, help you learn, write, or debug code (there are sadly only 8 support engineers for hundreds of thousands of sites worldwide)

So as a general point of direction, I would recommend picking up a course online (there are tons of free ones available!) for learning JavaScript! It’s a fun journey. In the meantime, here’s a quick answer. Please don’t take this and expect to run - I fully recommend doing a proper foundational course on the language.

fetch('/.netlify/functions/mostpopular', {
      method: 'POST',
      body: JSON.stringify({
        something: [
          "foo",
          "bar",
          "baz"
        ]
      })
    })

fetch takes a second argument of options for request data, which should contain a body for the actual body of the request, which ought to be stringified JSON. An array is a valid json value provided it has a valid key.

Give this a thorough read: Fetch API - Web APIs | MDN

1 Like

This helped to put me on track:

An array is a valid json value provided it has a valid key.

Here is an example to use “post” and an array with ntl functions:invoke - I use an alias nf: nf utils --port 80 --payload '{"t":[{ "category": "2"},{"category":1 }]}'