(JavaScript) Database fields fall back to previous value

I have a workflow initiated by a button click doing some java script calculations (Toolbox) and then saves the values to my DB. The strange thing is that the correct values only gets properly saved if I click the button twice, or if I run the workflow while debugging step by step or slow.

Seems like a timing issue, but that makes no sense to me because:

The result of my “Run Java script” calculation is stored in a “Java script to bubble” element.
The “make changes to thing” is saving this elements value.
On my page I created two text fields… one shows the “Things field value” and the other one the J2B elements value. They both show the correct result after button click…
But after a second or two, the text referencing the things field value switches to show the previous value stored in the field.
The text showing the J2B elements value is still showing the correct value ???

In my mind, if the text displaying the DB things field shows the correct value before reverting to the old one, it must somehow be written to the DB. The workflow tells me it is.

Has anyone encountered a similar problem ? Is it a bug ?

Step 1: (this is just creating and stingifying my JSON string for saving in a text field… Works fine/saves fine)
let myObject = ;
let counter = List LENGTH (J2B)'s value;

let tankName = [“List tankname (J2B)'s value list join with “,””];
let tankVolume = [List tankvolume (J2B)'s value list];
let tankStatus = [“List tankstatus (J2B)'s value list join with “,””];
let temperature = [List temperature (J2B)'s value list];
let bg = [“List bg (J2B)'s value list join with “,””];
let comment = [“List comment (J2B)'s value list join with “,””];
let animal = [List animal (J2B)'s value list];
let egg = [List egg (J2B)'s value list];
let eggPst = [List egg_pst (J2B)'s value list];
let chlorella = [List chlorella (J2B)'s value list];
let ff = [List ff (J2B)'s value list];
let pads = [List pads (J2B)'s value list:formatted as text];
let medication = [List medication (J2B)'s value list:formatted as text];
let experiment = [List experiment (J2B)'s value list:formatted as text];
let flow = [List flow (J2B)'s value list];
let type1 = [“List type1 (J2B)'s value list join with “,””];
let type2 = [“List type2 (J2B)'s value list join with “,””];

for (i=0 ; i < counter; i++){
let tankObject = {
tankname : tankName[i],
tankstatus : tankStatus[i],
animal : animal[i],
egg : egg[i],
egg_pst : eggPst[i],
chlorella : chlorella[i],
ff : ff[i],
temperature : temperature[i],
bg : bg[i],
type1 : type1[i],
type2 : type2[i],
flow : flow[i],
experiment : experiment[i],
pads : pads[i],
medication : medication[i],
comment : comment[i],
tankvolume : tankVolume[i]
}
myObject.push(tankObject);
}

let myJson = JSON.stringify(myObject);
bubble_fn_thisJSON(myJson);

STEP 2 : This is the calc that returns the values that im having a hard time getting to save correctly
const thisJSON = JSON.parse(‘Current Workflow text’);

//CALCULATE AMOUNT OF NEW ANIMALS
let newAnimals = thisJSON
.filter( tank => tank.tankvolume === 2000 )
.reduce( ( tot, add ) => tot + add.animal, 0) * 2 * 1000 * 1000;

//CALCULATE HOW MANY ANIMALS ARE DRAINED
let outAnimals = thisJSON
.filter ( tank => (tank.tankstatus === “M” || tank.tankstatus === “E” || tank.tankstatus === “K”))
.reduce((tot, add) => add.tankstatus !== “M” ? tot + add.animal : tot + add.animal * 1.2, 0) * 1000 * 1000;

//CALCULATE CHLORELLA AMOUNT PER DAY
let chlorellaTotal = thisJSON
.reduce ((tot , add) => tot + add.chlorella, 0) * 60 * 24 / 1000;

bubble_fn_dyrNye(newAnimals);
bubble_fn_dyrUt(outAnimals);
bubble_fn_chlTot(chlorellaTotal);

STEP 3 : Saves all the values (Everything but the three last values saves fine)

I find the problem strange because after button click → workflow… The J2B element on the page still holds the correct value…but the DB value flips back to it’s old state after a couple of seconds

Not easy to debug such a thing either… The server logs does not hold any information on whats going on

Any help/insights highly appreciated :wink:

Hi Bramat!

I don’t have a solution for you, but did experience the same thing a while back. I reported to Bubble, but I think they pointed me back to Misha, who is not currently active in the forum.

Back then I put a test together to see what was going on.

What you can see below is a button that generates a random number (just an easy way to see the changes.) And a second button that uses a js2bub element to save a value to the database. What happens is the value in the db, is always one behind the value of the random # element.
js%202%20bub

The workflows are basic:


And yes, when I run through step-by-step in the debugger, it works.
I also tried a pause action between the js run and the make changes action, but that made no difference.

I ended up going a different direction to keep moving forward.

This might just be a case of the values being used from when the initial event fires.

So no solution, but at least you know you’re not alone. :slight_smile:

–Ken



Looking to accelerate your app development?

Development of Advanced Apps at https://uniqueideas.com or schedule a free intro session :gift:

Ken Truesdale
LinkedIn

Thank you for your input Ken…

Managed to achieve the same result moving the calculations from a “run javascript” to separate Toolbox expressions on th page

thank you for posting this, i thaught i was going :exploding_head:

Same config, as

I have a workflow initiated by a button click doing some java script calculations (Toolbox) and then saves the values to my DB. The strange thing is that the correct values only gets properly saved if I click the button twice, or if I run the workflow while debugging step by step or slow.

Only worked when on step by step debugging

1 Like

I have this exact same issue currently and have reported it to bubble.

To confirm, it’s not the toolbox plug-in.

I created a custom plugin that passes JS variable to bubble and I too have to click the button twice which runs the workflow twice to get the value into a thing.

Other observations: the JS value renders instantly to a text element for example so the issue is with saving it to a thing. Another observation, if I put the workflow steps in twice (to mimic clicking the button twice) it still doesn’t save the data to the thing.

Hopefully someone from the bubble team checks this out soon as it’s cropped up a few times now.

And did you receive a reply on the subject ?

FYI, i’m in this exact same behaviour, your are not alone !

Other observations: the JS value renders instantly to a text element for example so the issue is with saving it to a thing.

cc : @carla & @peterj what is the official way to interact with database with JS calculated values?

@lucas.ar

Got a response. Most likely because it’s involving a “3rd Party Plugin”, it’s not a priority.

" We are not able to offer support for custom code at this time, though we certainly recommend that you post about it on our forum. In particular, the Plugin Builders section of the forum is great for posting your question or searching through other threads. As of right now, we’re not experiencing any platform bugs related to publishing plugin states or saving data and the forum is a great way to get help from other users who have experience building plugins. If you do observe an issue saving data in the app that can be isolated from the custom plugin and its code, feel free to reach back out to us with step-by-step instructions we can use to reproduce it."

After further research, this issue does appear to be a potential snag of Bubble if you wish to use the data beyond the initial workflow step.

Basically, if you pass the JS variable back in to bubble as a published state, it will work correctly for the current workflow step but you will experience the issues when trying to use this in an additional workflow step (e.g updating a thing).

Other notables: My bubble app was cloned from another app. Whilst I don’t think that would be a factor, you never know.

I believe there maybe a workaround for my use case that I pass the Plugin the “thing” I want updating and use JS code to trigger a server side action to update the “thing” with my JS variable but that’s a little beyond me currently (and not sure if technically possible currently?)

Unwilling to give in, I continued my quest for a solution and found one for my use case.

Regarding the whole discussion of async, letting bubble know and seeing a lag of always being 1 behind, I decided to use an event.

So rather than using a workflow to trigger my element action and then create/update a thing using just the published state, I broke it into 2 workflows.

The first workflow would now just handle the element action and publishing a state.

The second workflow is a triggerEvent that I triggered at the end of the Element Function in my plugin that was publishing the exposed state. I created a simple event and that runs at the end of the element action. Back in bubble, the workflow is a simple “When plugin element action is done…” and that’s when I updated/created a thing in Bubble and the data is there on time.

Problem solved for me.

Hope it helps other use cases.

1 Like

For anyone else running into this weird “saving previous value to databse” issue, I’ve been tearing my hair out for days trying to figure this out and have finally found a solution!

  1. Put the Run javascript action in one workflow and whatever js you are trying to run
  2. Set the javascripttoBubble page element “Trigger event” to yes
  3. Add an event “A Javascript to Bubble event”
    Screen Shot 2024-02-12 at 3.12.08 PM
  4. Put the workflow that you want to make changes to the database with into the javascript to bubble event. This fixed my issue with saving the previously run javascript value. Hope this helps someone else!

I’m using this to take a month and a year and create a unix timestamp then save it as a date in the database. If needing this solution ping me :+1:

1 Like

Thanks @arensbpa , encountered this behaviour and your post saved me hours (if not more) of chasing this down!

1 Like

Glad it helped!