使用JavaScript提取和Eventbrite API显示活动的场所名称

时间:2019-01-12 12:17:40

标签: javascript api promise fetch eventbrite

我正在创建一个大学项目,以显示利兹正在发生的事件。到目前为止,我已经能够显示有关事件的信息,但是我还想显示使用不同方法的场地名称。

我已经通过事件信息中找到的场地ID设法获得了场地名称,并且这些名称已正确显示在控制台中。现在,我希望能够在ui.js的HTML模板中显示会场名称。我认为这是一个简单的问题,我只是想不通!

eventbrite.js

class EventBrite {
    constructor() {
        this.auth_token = 'Example';
        this.orderby = 'date';
        this.city = 'Leeds';
    }

    //Load Leeds events from API
    async loadAPI() {
        const initalEvents = await fetch(`https://www.eventbriteapi.com/v3/events/search/?sort_by=${this.orderby}&location.address=${this.city}&token=${this.auth_token}`);

        const events = await initalEvents.json();

        return {
            events
        }
    }

    async loadVenue(venueID) {
        const initalVenue = await fetch(`https://www.eventbriteapi.com/v3/venues/${venueID}/?token=${this.auth_token}`);

        const venue = await initalVenue.json();

        return venue;
    }
}

app.js

const eventbrite = new EventBrite();
const ui = new UI();

eventbrite.loadAPI()
    .then(events => {
        //Check for events
        const eventsList = events.events.events;

        //Print venue IDs
        for(let i = 0, l = eventsList.length; i < l; i++) {
            const venueID = eventsList[i].venue_id;

            eventbrite.loadVenue(venueID)
            .then(venue => {
                const venueName = venue.name;
            })
        }
        ui.displayEvents(eventsList);
    }
)

ui.js

class UI {
    constructor() {
        this.init();
    }
    init() {
        this.printCategories();

        this.result = document.getElementById('result');
    }

    //Display events
    displayEvents(events) {   
        //Build template
        let HTMLTemplate = '';
        events.forEach(eventInfo => {
            HTMLTemplate += `
                <div class="row">
                    <div class="column">
                        <p class="event">${eventInfo.name.text}</p>
                        <p class="event">${eventInfo.start.local}</p>
                    </div>
                </div>
            `;
        });
        this.result.innerHTML = HTMLTemplate;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这应该可行:

eventbrite.loadAPI()
    .then(async events => {
        //Check for events
        const eventsList = events.events.events;

        //Print venue IDs
        for(let i = 0, l = eventsList.length; i < l; i++) {
            const venueID = eventsList[i].venue_id;

            let venue = await eventbrite.loadVenue(venueID);
            eventsList[i].venue = venue.name
        }
        ui.displayEvents(eventsList);
    }
)

但请注意:此代码已被阻止。场地一次加载一次。要同时使用它们,您应该使用Promise.all

寻找解决方案

编辑:全部承诺示例

eventbrite.loadAPI().then(events => {
    //Check for events
    const eventsList = events.events.events;
    let venuesPromises = eventsList.map(x => eventbrite.loadVenue(x.venue_id))
    return Promise.all(venuesPromises).then(x => {
        //x should contain all venues for the events. 

        const events_with_venues = 1 //TODO: merge events with venues
        return events_with_venues
    })
})