我正在嘗試向希望以YYYYMMDD格式發送時間對象的FourSquare API發出請求。我寫了一個產生時間對象的函數,但是我在使用React應用程序時遇到了問題。React組件生命週期API請求
我讓我的Profile
組件中的呼叫:
'use strict'
import React, { Component } from 'react'
import request from 'superagent'
import TimeService from './services/time'
import config from './config'
class Profile extends Component {
componentDidMount() {
TimeService.getCurrentTime((err, currentDate) => {
this.setState({
time: currentDate
})
})
}
componentWillMount() {
let currentTime = this.state.time
let venue = '4dd64e9de4cd37c8938e7b83'
//let venue = this.props.venueId
let clientId = config.CLIENT_ID
let clientSecret = config.CLIENT_SECRET
request.get(`https://api.foursquare.com/v2/venues/${venue}/hours/?\&client_id=${clientId}&client_secret=${clientSecret}&v=${currentTime}`)
.accept('json')
.end((err, res) => {
if(err) return
let result = res.body
console.log(res.body)
console.log(result.response.hours.timeframes)
})
}
當我在Chrome加載應用程序,我有錯誤消息:bundle.js:44492 Uncaught TypeError: Cannot read property 'time' of null
我的猜測是,我需要重新排序功能在組件安裝方法中,但我不確定如何。
我們處理了很多與從React生命週期方法中獲取數據相關的類似競爭條件。最後,我們決定將它抽象爲一個庫,以便我們的開發人員不需要考慮任何這些問題:https://github.com/DigitalGlobe/jetset – glortho