javascript - 如何将外部rest api数据作为记录数组传递给模型?
问题描述
我正在尝试使用我创建的 .net core 2.2 web api 并在 Google App Maker App 中显示 json 数据、键和值。我的 js 非常非常弱(因为我一直在尝试重新调整主要示例的用途,但没有真正的进展)。
我已经解决了许多问题以达到最终的错误(我希望如此)。
当我将一些 REST API 数据提交到数据存储区时。它要求:
- 一个数组(在js中我猜数组是对象...谁知道)
- 一个键(不知道如何在不从我为我的数据创建的 js 对象数组 中返回一个完整项目的情况下制作一个键)
- 对于对象的某种形式的映射(我想我已经克服了这个要求..不确定。这很可能会在我修复下面的错误后卷土重来)
错误:
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,因为我看到了明确的需求!我准备好了我的书和在线课程。如果我能在这个问题上得到一些详细的帮助,它现在会给我的项目一些新的生命,而不是从现在开始的很长一段时间。谢谢你们!
解决方案
我相信你的问题在这里:
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
推荐阅读
- python - 为什么我在 Tensorflow 中的逻辑回归分类器没有学习?
- javascript - 如何使用 Jest 测试处理来自外部变量的数据的函数
- http - 如何将来自 HTTPREAD 的数据存储到变量中?
- dns - 用于 Web 配置的正确 CNAME
- r - 如何获得F1、精度、召回和混淆矩阵
- c# - C# 游戏服务器刻度
- snowflake-cloud-data-platform - 向角色添加全局权限时出现问题
- javascript - p元素的Javascript不透明度过渡
- c# - ISet 的命名空间冲突:System.Collections.Generic 和 Iesi.Collections.Generic
- python - ImportError: No module named xgboost and package already installed