簡體   English   中英

使用Appcelerator Titanium將ID傳遞給函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM