首页 > 解决方案 > 如何将外部rest api数据作为记录数组传递给模型?

问题描述

我正在尝试使用我创建的 .net core 2.2 web api 并在 Google App Maker App 中显示 json 数据、键和值。我的 js 非常非常弱(因为我一直在尝试重新调整主要示例的用途,但没有真正的进展)。

我已经解决了许多问题以达到最终的错误(我希望如此)。

当我将一些 REST API 数据提交到数据存储区时。它要求:

错误:

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.

2019 年 9 月 22 日星期日 15:42:46 GMT-700

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

2019 年 9 月 22 日星期日 15:42:46 GMT-700

Executing query for datasource Weather failed.

服务器端代码

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;
  }

客户端代码

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

客户端代码

return calculateWeatherModel_();

提前感谢您在这里提供的所有详细帮助..我完全不知所措,将不得不推迟一个关键项目。

我现在将学习并开始掌握 JS,因为我看到了明确的需求!我准备好了我的书和在线课程。如果我能在这个问题上得到一些详细的帮助,它现在会给我的项目一些新的生命,而不是从现在开始的很长一段时间。谢谢你们!

标签: javascriptgoogle-app-maker

解决方案


我相信你的问题在这里:

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;

这是一种错误的做法。它应该是这样的:

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

显然,为了让它发挥作用,您的计算模型应该有一个预测字段和一个城邦字段。

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


推荐阅读