【问题标题】:Undefined function by using es6 and react使用 es6 的未定义函数并做出反应
【发布时间】:2017-10-16 22:47:40
【问题描述】:

我在从其他文件调用函数时得到一个未定义,我正在使用 webpack、babel、es6 和 react,根据标准我认为我做的事情是正确的,但这就是我正在做的在控制台中查看

TypeError: _openWeatherMap.getTemp(...) 未定义[Saber más] bundle.js:25033:9 得到 XHR response from request [HTTP/1.1 200 OK 232ms] //无论未定义,请求都已发出

这是我的文件

//open-weather-map.js

import axios from 'axios';

const OPEN_WEATHER_MAP_URL = 'http://api.openweathermap.org/data/2.5/weather?appid=*************&units=metric';

export function getTemp(location) {
  var encodedLocation = encodeURIComponent(location);
  var requestUrl = `${OPEN_WEATHER_MAP_URL}&q=${encodedLocation}`;
  axios.get(requestUrl).then((res) => {
    if(res.data.cod && res.data.message) {
      throw res.data.cod;
    } else {
      return res.data.main.temp;
    }
  }, (res) => {
    throw (res && ((res.response && res.response.data && (res.response.data.message || res.response.data)) || (res.code))) || res;
  });
};


//weather.js


import React, { Component } from 'react';
import WeatherForm from 'weather-form';
import WeatherMessage from 'weather-message';
import { getTemp } from 'open-weather-map';

class Weather extends Component {
  constructor(props) {
    super(props);
    this.state = {
      location: 'Miami',
      temp: 88
    };
  }
  handleSearch(location) {
    var that = this;

    getTemp(location).then((temp) => {
      that.setState({ location, temp });
    }, (err) => {
      alert(err);
    });
  }
  render() {
    let {location, temp} = this.state;
    return (<div>
      <h3>Weather component</h3>
      <WeatherForm onSearch={this.handleSearch.bind(this)}/>
      <WeatherMessage location={location} temp={temp}/>
    </div>);
  }
}

export default Weather;


//webpack.config.js
module.exports = {
  entry: './app/app.jsx',
  output: {
    path: __dirname,
    filename: './public/bundle.js'
  },
  resolve: {
    root: __dirname,
    alias: {
      main: 'app/components/main.js',
      nav: 'app/components/nav.js',
      weather: 'app/components/weather.js',
      about: 'app/components/about.js',
      examples: 'app/components/examples.js',
      'weather-form': 'app/components/weather-form.js',
      'weather-message': 'app/components/weather-message.js',
      'open-weather-map': 'app/api/open-weather-map.js'
    },
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [{
      loader: 'babel-loader',
      query: {
        presets: ['react', 'es2015', 'stage-0']
      },
      test: /\.jsx?$/,
      exclude: /(node_modules|bower_components)/
    }]
  }
};

我希望你能帮助我,因为我在这里浪费了我的机会,提前非常感谢你的帮助。

【问题讨论】:

  • 这只是一个问题:如果你将它添加到构造函数中:this.handleSearch = this.handleSearch.bind(this) 并将它用于 onSearch 属性:this.handleSearch,那么你将获得更好的性能,因为 React 赢了t 每次渲染 Weather 组件时都重新创建函数。

标签: javascript reactjs webpack ecmascript-6 babeljs


【解决方案1】:

您需要在您的getTemp 方法中返回axios.get(...)

函数的隐式返回值为undefined。因此,您尝试访问 .then()undefined,因此出现错误。

//open-weather-map.js

import axios from 'axios';

const OPEN_WEATHER_MAP_URL = 'http://api.openweathermap.org/data/2.5/weather?appid=*************&units=metric';

export function getTemp(location) {
  var encodedLocation = encodeURIComponent(location);
  var requestUrl = `${OPEN_WEATHER_MAP_URL}&q=${encodedLocation}`;

  return axios.get(requestUrl).then((res) => {
    if(res.data.cod && res.data.message) {
      throw res.data.cod;
    } else {
      return res.data.main.temp;
    }
  }, (res) => {
    throw (res && ((res.response && res.response.data && (res.response.data.message || res.response.data)) || (res.code))) || res;
  });
};

【讨论】:

  • 非常感谢这正是问题所在,下次我会回报承诺
  • 很高兴为您提供帮助。快乐编码! :)
猜你喜欢
  • 1970-01-01
  • 2018-04-10
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 2018-08-04
  • 1970-01-01
相关资源
最近更新 更多