0
我對Node.js非常陌生,以前一直沒有用過json數據,所以真的希望你能幫助我。如何從多個url頁面獲取json數據Node.js?
我想從特瑪的API的所有事件信息和具體的變量添加到MongoDB的。但是,我目前使用的API'限於每頁200個事件。因此,我不可能將活動信息與場地信息連接起來,因爲這些信息單獨添加到mongoDB中,並沒有詳盡列出所有活動和場地信息(由於缺少活動和場地數據而無法連接到ID)。
因此,我的問題是關於我如何能得到的所有頁面到我的數據庫在一次?
,我至今寫的代碼看起來像下面的東西:
app.get('/tm', (req, res) => {
axios // getting venues
.get('https://app.ticketmaster.com/discovery/v2/venues.json?apikey=myApiKey&page=0&size=200&countryCode=DK')
.then(response => {
const venuesToBeInserted = response.data._embedded.venues.map(venue => { // preparing venues
return {
sourceID: venue.id,
venue: venue.name,
postalCode: venue.postalCode,
city: venue.city.name,
country: venue.country.name,
countryCode: venue.country.countryCode,
address: !!venue.address ? venue.address.line1 : null,
longitude: !!venue.location ? venue.location.longitude : null,
latitude: !!venue.location ? venue.location.latitude : null,
source: 'ticketmaster'
}
})
// Create all venues at once
Venue.create(venuesToBeInserted).then(venues => {
console.log("venues inserted")
axios // getting events and shows - note the page parameter in the api link
.get('https://app.ticketmaster.com/discovery/v2/events.json?apikey=myApiKey&countryCode=DK&size=200&page=0')
.then(response => {
const eventsToBeInserted = response.data._embedded.events.map(events => { // preparing events
const event = events._embedded.attractions[0]
return {
sourceID: event.id,
name: event.name,
slug: slugify(event.name).toLowerCase(),
tags: !!event.classifications ? [event.classifications[0].genre.name, event.classifications[0].subGenre.nam] : [], // duplicate genres occur
// possible tags from ticketmaster: type and subtype
}
})
// Create all events at once
Event.create(eventsToBeInserted).then(events => {
console.log("events inserted")
const showsToBeInserted = response.data._embedded.events.map(show => {
const event = events.find(event => event.sourceID == show._embedded.attractions[0].id);
const venue = venues.find(venue => venue.sourceID == show._embedded.venues[0].id);
if (!!event && !!venue) {
return {
event: event._id,
venue: venue._id,
timezone: show.dates.timezone,
dateStart: !!show.dates.start.dateTime ? show.dates.start.dateTime : show.dates.start.localDate,
tickets: !!show.priceRanges ? {
minPrice: show.priceRanges[0].min,
maxPrice: show.priceRanges[0].max,
currency: show.priceRanges[0].currency
}: {}
}
}
})
// Let's see what we have created in the database
Venue.find({}).select({
name: 1,
slug: -1
}).limit(10).populate('event').populate('venue').then(events => {
console.log(util.inspect(events));
}).catch(err => {
console.error(err);
});
}).catch(err => {
console.error(err)
})
}).catch(err => {
console.error(err)
})
}).catch(err => {
console.error(err)
});
}).catch(err => {
console.error(err)
})
})
編輯
使用傑克建議給了我一個錯誤(方法錯誤:請求失敗狀態碼爲401)。我試圖在網上搜索它,但我無法弄清楚爲什麼會發生錯誤。在我的console.log中查看部分錯誤消息的圖片。
感謝您的幫助傑克。但是我收到一條錯誤消息。請參閱我在上面的帖子中的編輯。我不知道錯誤是什麼意思.. –
401意味着你是未經授權的,它看起來像錯誤消息說你的MongoDB是安全的,並需要用戶名/密碼來建立與Mongoose的連接。 –
看起來您並未在請求URL中設置真實的API密鑰,該值在響應中爲'apiKey',您需要發送在請求URL中提供給您的API密鑰。 –