Server-side actions plugins. OPEN BETA

Hi fellow bubblers,

I joined bubble last week and I’m glad to see support for server side actions, exciting times!

I gave it a shot and was able to make a first action that returned some hard coded values.

The next step was to try to sum up some values stored in the DB:

  1. Make a bubbly data api call
  2. Iterate over the results and keep a running total
  3. Return that total to display it in bubble

It looks like context.request is asynchronous so I tried to use the await keyword but that did cause a syntax error (This code cannot be interpreted as javascript)

I noticed the context did contain an async function but it takes a callback so that’s probably to start an async operation, not to wait for one?

Am I approaching this wrongly? Should I return some kind of promise that will eventually resolve?

function(properties, context) {
  
  var options = {
      url: 'https://myapp.bubbleapps.io/version-test/api/1.1/obj/job',
      headers: {
        // TODO: Auth
      }
	};

  var total = 0;
  function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
      var data = JSON.parse(body);
      
      for (var i = 0; i < data.response.results.length; i++) {
        var job = data.response.results[i];
      	total += job.amount_number;    
      }
    } else {
      total = -1;
    }
  }

  // Asynchronous call, won't be finished when we return
  context.request(options, callback);
  
  // This triggers a syntax error
  // async context.request(options, callback);
  
  var result = {
    total : 100 + total
  };
  
  return result;
}
1 Like