Cannot pass array from ServerConnect to AppConnect

I am calling an API from server connect. The API is returning a String that includes several \n\n.
In server connect, I am trimming this string and splitting it into an array as follows:

    {
         "name": "api_outputtext",
         "module": "core",
         "action": "setvalue",
         "options": {
           "value": "{{api1.data.choices[0].text.trim()}}"
         },
         "meta": [],
         "outputType": "text",
         "output": true
       },
       {
         "name": "output_array",
         "module": "core",
         "action": "setvalue",
         "options": {
           "value": "{{api_outputtext.split(/\n\n/)}}"
         },
         "meta": [],
         "outputType": "array",
         "output": true
       }

I can see the correct answer in the developer tools of my browser:

["Customer Segments: The customer segments for this startup is small businesses that require their own software data to carry out their activities. They will be primarily located in North America, but this startup is ready to support businesses located worldwide. The customer segment could be businesses of any sizes, ranging from startups to Fortune 500 companies.","Problem: With the constant advancement of technology, small businesses need to find a way to keep up. Many companies do not have the ample resources for R&D and software development to stay competitive and up to speed. Thus, many of them remain stuck with the outdated and often times inefficient systems and softwares that have limited capabilities."]

However, when retrieving this data on the Frontend through a POST form, I get an undefined value:

<form id="form_1" method="post" is="dmx-serverconnect-form" >action="/api/Folder/generateText" dmx->on:success="browser1.alert(form_1.data.output_array[0])">

I tried several combinations without success, is it related to a bug or to a mix between JS functions and DMX functions in my code? Thank you in advance for your guidance

From what I gather, you are wanting to perform a read function which requires a get from the server.

For this, you need to collect the data by using a server connect. You can then use the data within server connect in any manner that you wish.

Thank you Ben, I wanted to use POST as my parameter might be longer than 2,048 characters, but for testing I just tested now with GET and it is the same issue. Do you recommend I look for the problem anywhere else?

This is the response I am getting from the server which looks fine to me: (Copy-pasted from my browser developer tools)

["TItle 1: Value 1.","Title 2: value2","...."]

And this is how I configured the ServerConnect:

<dmx-serverconnect id="serverconnect1" url="/api/OpenAI/generateCanvas" dmx-param:getstartupidea="Prompt.value" noload="true" dmx-on:success="accordion1_collapseOne.text_1.setValue(serverconnect1.data.api_outputtext)[0]"></dmx-serverconnect>

What do you think I am doing wrong here?

It’s not entirely clear to me what are you trying to do exactly?
Do you want to get the response from your server action and show it on the page or is it something else?

form.submit() doesn’t return data but to be honest I haven’t looked into how a SC form works. It probably already does an AJAX call.

Anyway, maybe try adding a response step in your SC to force the response of your data. I don’t know how that will go as I have never needed to read data from a POST form.

Thank you all for your support. It finally worked out even with a POST (Form submit).
The root cause was that I had on server connect a “Response” action at the end that was messing with the format of the JSON. I removed the “Response” step and it works like a charm. Thanks again

2 Likes