【问题标题】:TypeError: undefined is not an object (evlauating 'newData[0].flag')TypeError: undefined is not an object (evlauating 'newData[0].flag')
【发布时间】:2021-03-15 05:01:52
【问题描述】:

我集成了一个名为 rn-country-picker 的依赖项,并在我的 react native 应用程序中使用。它之前工作正常,但现在它抛出错误“TypeError:未定义不是对象(evlauating 'newData[0].flag')。”我无法弄清楚错误的确切原因。

我使用该库的部分有以下代码:

{/* COuntry */}

      <View style={styles.container}>
        <Text style={styles.titleText}>Select your country</Text>

        <CountryPicker
          disable={false}
          animationType={"fade"}
          containerStyle={styles.pickerStyle}
          pickerTitleStyle={styles.pickerTitleStyle}
          selectedCountryTextStyle={styles.selectedCountryTextStyle}
          countryNameTextStyle={styles.countryNameTextStyle}
          pickerTitle={"Select your country"}
          searchBarPlaceHolder={"Search......"}
          hideCountryFlag={false}
          hideCountryCode={false}
          searchBarStyle={styles.searchBarStyle}
          countryCode={this.state.mCountryCode}
          selectedValue={this._selectedValue}
        />
      </View>

这是 Vishal Dhanotiya 的一个依赖项,您可以从以下网址找到代码:https://github.com/vishaldhanotiya/rn-country-picker/blob/master/index.js

它在第 84 行显示错误。我只从组件中删除了道具,除此之外我没有触及任何其他代码。以前很好,但是突然抛出这个错误。请帮忙。

【问题讨论】:

    标签: javascript react-native typeerror


    【解决方案1】:

    是的,当然,它给出了例外,因为您像访问数组一样访问newData,它可能有一个布尔值。

    const newData = CountryJSON.filter(function (item) {
          const itemData = item.callingCode;
          const textData = defaultText;
          return itemData === textData; // it is returning boolean value here either true or false
        });
    
        return (
          <View>
            ........
              {hideCountryFlag ? null : (
                <Image
                  source={{uri: newData[0].flag}} // It is an exception because it's either tru or false not array of course
                  style={styles.countryFlagContainer}
                />
              )}
              ......
          </View>
        );
    

    【讨论】:

    • 你能帮我解决这个问题吗@Nooruddin
    猜你喜欢
    • 1970-01-01
    • 2020-08-15
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多