首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未处理的承诺拒绝: ReferenceError找不到变量

未处理的承诺拒绝: ReferenceError找不到变量
EN

Stack Overflow用户
提问于 2022-02-14 16:06:00
回答 1查看 3.6K关注 0票数 0

我对Javascript有点陌生,我不能解决这个错误.为了根据用户输入从API服务器获取数据,我尝试使用异步函数。默认的this.state.location"Boston"。我正在尝试将this.state.location更改为用户输入值。不知道为什么我会犯这个错误..。我正在使用异步等待好吗?

下面是代码:

代码语言:javascript
复制
import React, { Component } from "react";
import { View, Text, Button, StyleSheet, TextInput } from "react-native";

const Separator = () => <View style={styles.separator} />;

class Home extends Component {
  constructor(props) {
    super(props);

    this.state = {
      location: "Boston",
      result: [],
      isLoading: true,
    };
  }

  async getWeatherData() {
    // Weather API
    // documentation: https://www.visualcrossing.com/resources/documentation/weather-api/timeline-weather-api/
    const url =
      "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/" +
      this.state.location +
      "/next7days?unitGroup=us&key&contentType=json";
    const result = await fetch(url);
    const resultJson = await result.json();
    return resultJson;
  }

  weatherData = async () => {
    let result = await getWeatherData();
    return result;
  }

  async componentDidMount() {
    let result_data = await weatherData();
    this.setState({ result: result_data, isLoading: false });
  }

  render() {
    if (this.state.isLoading) {
      return (
        <View>
          <Text>Loading data...</Text>
        </View>
      );
    } else {
      return (
        <View
          style={{
            flex: 1,
            flexDirection: "column",
            alignItems: "center",
            justifyContent: "center",
          }}
        >
          <TextInput
            style={styles.input}
            placeholder="Enter location"
            onChangeText={(location) => this.setState({ location })}
            value={this.state.location}
          />
          <Button
            title="Weather"
            onPress={() => {
              this.props.navigation.navigate("Weather", {
                day0_datetime: this.state.result.days[0].datetime,
                day0_icon: this.state.result.days[0].icon,
                day0_tempmin: this.state.result.days[0].tempmin,
                day0_tempmax: this.state.result.days[0].tempmax,
                day1_datetime: this.state.result.days[1].datetime,
                day1_icon: this.state.result.days[1].icon,
                day1_tempmin: this.state.result.days[1].tempmin,
                day1_tempmax: this.state.result.days[1].tempmax,
                day2_datetime: this.state.result.days[2].datetime,
                day2_icon: this.state.result.days[2].icon,
                day2_tempmin: this.state.result.days[2].tempmin,
                day2_tempmax: this.state.result.days[2].tempmax,
                day3_datetime: this.state.result.days[3].datetime,
                day3_icon: this.state.result.days[3].icon,
                day3_tempmin: this.state.result.days[3].tempmin,
                day3_tempmax: this.state.result.days[3].tempmax,
                day4_datetime: this.state.result.days[4].datetime,
                day4_icon: this.state.result.days[4].icon,
                day4_tempmin: this.state.result.days[4].tempmin,
                day4_tempmax: this.state.result.days[4].tempmax,
              });
            }}
          />
          <Separator />
          <Button
            title="Daily Fashion"
            disabled
            color="#f194ff"
            onPress={() => {
              this.props.navigation.navigate("DailyFashion", {
                day0_datetime: this.state.result.days[0].datetime,
                day0_icon: this.state.result.days[0].icon,
                day0_tempmin: this.state.result.days[0].tempmin,
                day0_tempmax: this.state.result.days[0].tempmax,
              });
            }}
          />
          <Separator />
        </View>
      );
    }
  }
}

const styles = StyleSheet.create({
  separator: {
    marginVertical: 8,
  },
  input: {
    height: 40,
    margin: 12,
    borderWidth: 1,
    padding: 10,
  },
});

export default Home;

这是我遇到的错误:

代码语言:javascript
复制
[Unhandled promise rejection: ReferenceError: Can't find variable: weatherData]
at node_modules/react-native/Libraries/Core/setUpReactRefresh.js:43:6 in Refresh.performReactRefresh
at node_modules/metro-runtime/src/polyfills/require.js:655:10 in setTimeout$argument_0

谢谢你的帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-14 16:52:50

weatherData是类实例上的一个属性。

它不是一个变量(范围内或其他方面)。

您需要使用this.weatherData访问它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71114809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档