Running Javascript, that "if true", changes field in DB

I don’t know Javascript (but can follow the syntax somewhat). I’m wondering if anyone has been able to achieve something like this, or if you could in Bubble with the JS Toolbox plugin:

  1. Run JS on page
  2. If something is true (like, if callback from JS = true) then change a field in the DB to true, or set a custom state.
  3. Then you are able to display certain things to that user, based on the JS being true.

Hopefully that makes sense, if not, I can elaborate a bit more. I’m trying to figure out if this needs to a be a custom plugin, or if it can simply be integrated into Bubble as-is. Tagging @supernaturally since you were so helpful in this thread on JS Javascript access bubble db and thought maybe you could share some insight?

If you can explain more what you are trying to look if true, we may help you without using any JS.
From what I understand, you are trying to access Bubble DB and check if something is true from this call. But this can be all done with native Bubble functions.

Sorry about that. Here’s an example…

I’m using a native wrapper for my app. The wrapper has capabilities to call native app functionality (say, on iOS - touch ID, face recognition, etc.). In order for me to make use of it in my web app, the wrapper comes with JS code I can use that will tell me if a user’s mobile device is currently employing (or capable of employing) this native functionality (touch ID, etc.).

So I’d like to be able to show an element (say, a button, or piece or text) to users, but only if the JS is “true”. I don’t really understand JS, but I think this would be an example…

function featureStatusCallback(data) {
if (data.available) {
// show user unique things, e.g. a button to start scanning
}
}

So I’m wondering how to replace “// show user unique things, e.g. a button to start scanning” with something that will call an event to change a user’s record in the DB. For example, under a User Type, I might make a field that says, “touchIDAvailable” - set it to yes/no - then when the JS runs, it will tell me whether the mobile app has that capability. If so, it will change the record in the DB to “yes”. At that point, I can show a button or text element conditionally, to user’s with touchIDAvailable=yes.

I hope that makes sense. I’m sorry for any way I’m complicating it, I just don’t understand JS and am looking to see how I can use it to find out certain available mobile features (through the native wrapper) then make Bubble respond accordingly.

The JS toolkit element can publish a value, e.g. Yes/No. You could set this in your JS and then display your button/text if the value from the JS element is Yes. Something along these lines might work.

Hi Jeremiah,

Louis has it right…you can run whatever js you want in the expression element from the Toolbox and just set the final result to true or false using a yes/no Bubble data type:

–Ken

3 Likes

Thanks Ken and @louisadekoya , I’m trying this out now. Still can’t get it to work, but your test example does work, so I think I just need to learn a bit of basic JS.

I’ll report back with my progress for anyone who might be helped by this thread.

1 Like

As @mebeingken, notes you can run JavaScript in the expression element (which returns a value to Bubble via the Expression’s value field), but you can also use Toolbox’s Run JavaScript action in a workflow that then passes the value back to Bubble via Toolbox’s JavaScript to Bubble element.

A very simple video explanation of how to use that action/element pair, can be found here:

OK, I’ve spent a while working on this, but still can’t crack it - though the concepts make sense to me. I think it’s my lack of knowledge of JS.

I put the following code in a JS Expression (element) on the page:

function featureStatusCallback(data) {
if (data.available) {
return true;
}
}

window.location.href = ‘appservice://feature/status?callback=featureStatusCallback’;

I then made a button, with the condition of only displaying when Element A’s value = yes. I’ve also tried running the JS on page load, and using a JS to Bubble element, still with no luck.

Any ideas as to how I can get this?