[英]Passing an ID to a function using Appcelerator Titanium
我在这里有些困惑,因此我需要一些指导。
基本上,我有一个与API对话并收集有关“游览”信息的应用程序。 第一个请求检索3个旅程(ID和名称)并将其存储在数据库中,然后第二个请求应遍历这3个旅程(使用Tour Detail API端点)并获取每个旅程的信息(包括lon / lat详细信息)游览。
现在,这很好。
我遇到的问题是,每个游览都有其自己的ID号。 不幸的是,当第二次和第三次旅行进入数据库时,它们使用的是与第一次旅行相同的ID号。
所以我猜我没有正确传递ID。
任何人都可以帮助正确进行此循环吗?
下面的代码用于整个功能。
// get tours and add them to DB
function loadTours() {
// get the species data
var loader = Titanium.Network.createHTTPClient({
timeout: 30000 /* in milliseconds */
});
loader.onload = function() {
// get the JSON response
var tours = JSON.parse(this.responseText);
var db = Ti.Database.open('myDB');
// truncate the tours table
db.execute('DELETE FROM tours');
for (var i = 0; i < tours.length; i++) {
// Get the data from the feed
var tourID = tours[i].nid;
var tourTitle = tours[i].node_title.toUpperCase();
Ti.API.info(tourID + ' ' + tourTitle);
// check if images have been uploaded
if (tours[i].app_image.uri) {
// removed
} else {
var tourImageFullPath = "";
}
// add the tours to the SQL
db.execute('INSERT INTO tours (tourNID, tourName, tourImageURI) VALUES (?,?,?)', tourID, tourTitle, tourImageFullPath);
// lets do a loop through and insert the tour details
var getTourDetails = Titanium.Network.createHTTPClient({
timeout: 30000 /* in milliseconds */
});
getTourDetails.onload = function() {
var tourDetails = JSON.parse(this.responseText);
db.execute('DELETE FROM tourdetails');
for (var i = 0; i < tourDetails.length; i++) {
var tourNID = tourID;
var tourSpeciesID = tourDetails[i].node_field_data_field_tour_item_nid;
var tourType = tourDetails[i].node_field_data_field_tour_item_type;
if (tourDetails[i].Latitude.length === 0){
var tourLat = '';
} else {
var tourLat = tourDetails[i].Latitude;
}
if (tourDetails[i].Longitude.length === 0){
var tourLon = '';
} else {
var tourLon = tourDetails[i].Longitude;
}
if (tourDetails[i].node_field_data_field_tour_item_type.length === 0){
var tourItemType = '';
} else {
var tourItemType = tourDetails[i].node_field_data_field_tour_item_type;
}
var tourItemTitle = tourDetails[i].node_field_data_field_tour_item_title.toUpperCase();
db.execute('INSERT INTO tourdetails (tourNID, tourItemID, tourLon, tourLat, tourItemTitle, tourItemType) VALUES (?,?,?,?,?,?)', tourNID, tourSpeciesID, tourLon, tourLat, tourItemTitle, tourItemType);
Ti.API.info('TOUR: ' + tourNID + ' ' + tourSpeciesID + ' ' + tourLon + ' ' + tourLat + ' ' + tourItemTitle + ' ' + tourItemType);
}
};
getTourDetails.open("GET", "https://www.myapi.com/tour?nid=" + tourID);
// change the loading message
MainActInd.message = 'Downloading Data';
// show the indicator
MainActInd.show();
getTourDetails.send();
}
// now load the map markers
getMapMarkers();
}; //onload - end the JSON fetch and process for species
// Sets the HTTP request method, and the URL to get data from
loader.open("GET", "https://www.myapi.com/tours");
// change the loading message
MainActInd.message = 'Downloading Data';
// show the indicator
MainActInd.show();
loader.onerror = function() {
// do something if an error
MainActInd.hide();
//alert('tours data error');
var errorDialog = Ti.UI.createAlertDialog({
message: 'We are having an issue importing the Tour data. Please try again later.',
title: 'Whoops'
});
errorDialog.show();
};
// Send the HTTP request
loader.send();
}; // end the get data function
出于安全原因,我不得不掩盖了一些内容,但是您应该了解我要实现的目标!
任何帮助将不胜感激!
西蒙
这里的问题是外循环完成(i在最后一个位置,tourID也是如此)。 循环之后,将调用send函数中的回调,您将在其中再次使用tourID。 但是那时它是最后一个ID,在您的info()中可见。 最快(且更安全)的方法是将id包含在服务器的实际回调中,因此:
var tourDetails = JSON.parse(this.responseText);
应该将tourID作为json字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.