Search code examples
javascriptgoogle-app-maker

How to pass external rest api data to a model as an Array of records?


I am trying to consume a .net core 2.2 web api i created and display the json data, keys AND VALUES, in a Google App Maker App. My js is very very weak (caz ive been trying to re-purpose the main example in googles docs for 7 days now with no real progress).

I have fought though many many issues to get to a final ERROR ( i hope).

When i commit some of the REST API data to the datastore. its ask for:

  • an array ( in js i guess arrays are objects...who knew)
  • a Key ( no idea how to make a key with out returning one FULL ITEM from the js object array i created for my data)
  • and for some form of mapping for the object ( i think i have overcome this requirement..not sure. this most likely will comeback after i fix the error bellow)

ERROR:

The function queryRecords must return an array of records, but the 
array contained an element that was not a record. Error: The 
function queryRecords must return an array of records, but the 
array contained an element that was not a record.

Sun Sep 22 15:42:46 GMT-700 2019

Executing query for datasource Weather: (Error) : The function 
queryRecords must return an array of records, but the array 
contained an element that was not a record.
at loadWeatherDataSource (CallWeatherService:6:27)
at Weather.LocationTextBox.onValueChange:1:1
at Weather.LocationTextBox.onAttach:1:14

Sun Sep 22 15:42:46 GMT-700 2019

Executing query for datasource Weather failed.

Server side code

function clearEmailForm(){


       var url= 'https://xxx.azurewebsites.net/api/xxxxxx/3';


          var response1 = UrlFetchApp.fetch(url);
          var api1= response1;







  return JSON.parse(api1);
      }  

 function cal() {
   var coordinates =  clearEmailForm(); 


     return {
      forecast: coordinates.Scene,
      citystate: coordinates.imageurl
    };
  }
function calculateWeatherModel_() {

  var response;
  try {
    response = cal(); 

  } catch (error) {
    throw new Error('Error Unable to locate provided city: \"' + response +  '\".');
  }

  if (response === null) {
    throw new Error('null Unable to locate provided city: \"' + response + '\".');
  }

  var forecastPeriods = response.forecast;
  var citystate = response.citystate;
  var arr = [];


     var record = app.models.Weather.newRecord();

     arr.push(record.forcast = forecastPeriods  ); 

      var record = app.models.Weather.newRecord();

    arr.push(record.citystate = citystate);



    return arr;
  }

Client side code

function loadWeatherDataSource() {
  app.datasources.Weather.load({
    failure: function (error) {
      displayTimedSnackBar(error.toString());
    }
  });
}

Client side code

return calculateWeatherModel_();

Thanks ahead of time for any and all detailed help here..I am at a complete loss and will have to delay a critical project.

I will now study and start to mastering JS as i see a clear need! I have my books and online course ready to go. if i can get some detailed help on this issue it will give my project some new life now instead of a long time from now. thank you all!


Solution

  • I beleive that your problem is here:

    var arr = [];
    
    var record = app.models.Weather.newRecord();
    
    arr.push(record.forcast = forecastPeriods  ); 
    
    var record = app.models.Weather.newRecord();
    
    arr.push(record.citystate = citystate);
    
    return arr;
    

    That is a wrong way of doing it. It should be like this:

    var arr = [];
    var record = app.models.Weather.newRecord();
    record.forcast = forecastPeriods;
    record.citystate = citystate;
    arr.push(record);
    return arr;
    

    Obviously, in order for that to work, your calculated model should have a forcast field and a citystate field.

    Reference: https://developers.google.com/appmaker/models/calculated#query_script_example