Output an object for server-side/client-side

I might be missing something obvious (and this might be a bit of a side track), but what’s the advantage of a server-side script if the text being processed is being input directly by the user?

I’ll try to explain myself.
My goal is to turn a piece of multiline text into things (specifically, laptop’s specifications list into rows of specifications thing).
I pull the text from the user’s text box, now comes the plugin’s part where it parses it and returns an array of a JSON of specification’s attributes and values, grouped by title.

Then I’m not sure what you mean when you say it doesn’t work “when using my variable”.

Because it’s hard (to my knowledge) to test my code on server-side, I copied it to client-side and used “console.log”. it seems to output what i wanted.
when I return to server-side, If I copy my results from console.log and manually return it, it works, so there’s no issue with the JSON building part or my code.
but if I just execute my code on server-side, then it returns empty results.

Did you step through - not just execute - the code when it provides incorrect output?

I know you can add a break point on client-side, but not on server-side.

Ok, but again, why does it need to run server-side? Maybe there’s no problem to solve here.

You might already understand this, but just to be clear… The technique described in this thread simply makes it easier to work with “complex” (custom) data types in the Bubble editor. It has nothing to do with the database.

This thread is the closest thing I found to get results from my plugin.
If I use server-side action without the API call, simply use my thing inside the ‘App type’


image
then I get empty results.

If I try using plugin’s “Elements” and Exposed states, then when I set
instance.publishState(“specification_object”, resarray);
when ressarry is an array


Then although as you can see in “inspect” it has value, the published state is returned empty.

@sudsy
What was that async fetching you were talking about? I think it might be the only solution to my problem.
How can I make it wait?

There is a lovely solution that we use to make an async function as a sync one.

const deasync = require('deasync');
let result;

a_library.async_function(params, (err, data) => {

  if (err) {
      throw new Error(err.message)
  } else {
      result = data
  }

  done = true
});

deasync.loopWhile(function () {
  return !done;
});

//do something with your data

Note: Use this method only for the server-side plugin actions since the Bubble uses Lambda that runs code in serverless containers. You should not use this method directly on a server.

1 Like

Thanks so much!
I just copy and paste this without any changes to the beginning of my code?

Nope. Wait a bit.

Can you please send me a file with all your current script?


OR FiddleJS
https://jsfiddle.net/Amitgeva/syrmbtj6/1/#&togetherjs=xhMV1tlm5i
Thanks!!

Just to cover the bases… If you’re trying it client-side in an exposed state (as you’ve indicated above), did you make the association with your custom type via the Properties editor as described here?

-Steve

Sure did

Nope, that’s not the Properties editor. That’s the plug-in editor. I’m talking about the Properties editor that the Bubble dev uses to build the app.

Hmm…
I’m really lost on where is the property editor?
Do you mean this?
image
(That’s the plugin’s element’s properties, where I specify the data type)

Apologies for not knowning what you mean

As the error states, your JS is invalid. You could try…

var response = {
   "_p_Tree.Leaf" : []
}

…and then…

response["_p_Tree.Leaf"].push()

…but why go through all that trouble when you could simply work with “normal” JS and then run it through @lottemint.md’s conversion utility as the last step before output.

Was it a reply to me or redvivi? (because I don’t see his message)

It was to @redvivi, but it seems their post has been deleted, which leaves my reply “dangling”. Kinda strange.

Did you get your custom type working? And yes, that’s what I was referencing - i.e. where you configure your element on the page. Not sure what else to suggest. There are a number of detailed steps, so following them closely is critical. Maybe retrace your steps. Sometimes a second or third pass is what it takes.

OK so I finally solved it.
The problem I experienced is how to return a Bubble’s object, as defined using the App type.
I don’t think Bubble supports that.
This is why this thread here exists.
So my solution - after I calculated my array, it needed to be transform to JSON format, which for that I used JSON.stringify
But that wasn’t enough, because now it’s just a piece of text, it needed to be converted to JSON object, so the missing piece was creating a new var with the command JSON.parse.
That’s it.
Thanks to all the helpers.

2 Likes

Hello everyone! :wave:t2:

There is an important update to this module.

Please check out the following link:

In short, Shotster provided a fix for the primitive arrays (value-only). I’ve already committed the fix. Thanks to Shotster for catching and fixing it out!

It looks like it works as expected now. Sorry about this one. I didn’t notice it.

@sudsy, can you please update your app (https://ctgen.bubbleapps.io/)? It looks like it uses the 1.0.2 version, not the latest (1.0.4).

@redvivi, thanks for pointing out the same issue. Can you have a look at this to ensure if that works now, please?

Let me know if you have any questions, please.

Thanks!

2 Likes

It should be up to date now. :slightly_smiling_face:

2 Likes

Thanks!

@redvivi

1 Like

It works.
Many thanks to both of you @lottemint.md & @sudsy.

2 Likes