Passing data from a field to a state

I’m creating a plugin where I want to pass data from a field I created on the plugin to an exposed state on the plugin. In the Bubble app, I will set the field dynamically to text from my Bubble app. I created the field and state, as well as added this code to the update function in the plugin.

function(instance, context) {
  let attempts = 0;
  const maxRetries = 5;

  function tryPublish() {
    const preload = instance.data.preload_selection;

    console.log(`update attempt ${attempts + 1}:`, preload);

    if (preload && preload.length > 0) {
      instance.publishState("selectionList", preload);
      console.log("update: Published preload_selection to selectionList");
    } else if (attempts < maxRetries) {
      attempts++;
      setTimeout(tryPublish, 200);  // retry after 200ms
    } else {
      console.warn("update: Gave up after 5 tries");
    }
  }

  tryPublish();
}

I’m using chatGPT to help with the code.

Unfortunately, the exposed state is empty in the Bubble app. When inspecting the element, the field shows the text, but the state is empty. Both the field and state are set to the same data type and the naming matches between the code and field/state settings in the plugin.

Where am I going wrong?

  1. You should keep the properties argument in the update function.
  2. You can only grab the values of fields in the update function. Which are stored in the properties argument.
  3. If preloadis supposed to contain the field value then you’re not sending anything. See point 2.

I suggest to learn more about Bubble plugins (it’s actually quite simple. Just extremely quirky) first before using AI. Most LLMs don’t understand Bubble or/and have outdated info about it.

Then you can use the instructions here as context for AI: My VSCode Copilot Bubble Plugin Instructions

Thanks, I was able to get a simple version working with this guidance. I need to spend more time understanding how the plugin editor works as you suggested.