Retrieve oldest 4 published records

Hey all, I’m loading items from my CMS using the api, and not seeing anything in the documentation about ordering rows by a certain parameter. For context, I’m limiting the response to 4 published records, but I’m getting the newest records first when I’d rather get the oldest 4 records. Is there a query parameter I can use for this?

The relevant code (React.js):

setInterval(async () => {
      console.clear();
      const apiUrl = new URL(
        `<https://data.plasmic.app/api/v1/cms/databases/${CMS_ID}/tables/${modelId}/query>`
      );
      apiUrl.search = new URLSearchParams({
        q: JSON.stringify({ where: { open: true }, limit: 4 }),
      }).toString();
      setIsBusy(true);
      console.log("KDS is checking for tickets!");
      // Do network stuff
      try {
        const response = await fetch(apiUrl.toString(), {
          headers: {
            // Plasmic CMS ID and CMS Public API token
            "x-plasmic-api-cms-tokens": `${CMS_ID}:${CMS_PUBLIC_TOKEN}`,
          },
        });
        const parsedResponse = await response.json();
        setOpenTickets(parsedResponse.rows);
        setIsBusy(false);
      } catch (error) {
        return;
      }
    }, 10000);

looks like this hasn’t been documented, but adding

order: "asc"

to the URLSearchParams object seems to have done the trick! Can we as users help contribute to docs?

Actually, this doesn’t seem to be the correct solution, as adding a new item makes it display first for some reason. Not sure what field these results are ordered by by default?

You can add this to the query:

{
  where: ...,
  order: [{field: "..", dir: "asc"|"desc"}]
}

sorry the documentation is a bit behind :sweat:

No biggie! You folks are killing it :heart:

Hey again Chung, I’m also facing an error with this PUT request, if you could take another look when you get a chance:

  async function closeTicket(rowId) {
    const apiUrl = new URL(`<https://data.plasmic.app/api/v1/cms/rows/${rowId}>`);
    // Do network stuff
    try {
      const updateJson = {
        data: {
          open: false,
        },
      };
      const response = await fetch(apiUrl.toString(), {
        method: "PUT",
        headers: {
          // Plasmic CMS ID and CMS Private API token
          "x-plasmic-api-cms-tokens": `${CMS_ID}:${CMS_SECRET_TOKEN}`,
          "content-type": "application/json",
        },
        body: JSON.stringify(updateJson),
      });
      const parsedResponse = await response.json();
      console.log(parsedResponse);
    } catch (error) {
      console.log(error);
    }
  }

This results in a CORS error:

ah thanks, we see the issue, will have to roll out a fix

Nice! I’ll just leave this open, no big rush just in development

hmm actually, are you trying to issue this query from the browser? We don’t usually recommend doing so, since that means you are exposing your secret token to the browser

No, using a vanilla react app deployed to vercel, and the secret key is in my envelope variables

*environment variables lol

hmm if the browser is making the call, the secret key must be provided, and your bundler may be baking the environment variable value into the build (or not, but then the call wouldn’t be authenticated)

Hmmm would this call normally be made server-side? I could probably use a Vercel serverless function and see where that gets me

Yeah you’d want to route through something on the server side that can keep a secret safe.

Sounds good, that explains the cors error. I’ll give that a shot!