在位置1 / Asyncstorage上以JSON响应Native-意外令牌o

时间:2019-06-28 01:06:28

标签: javascript json react-native

我迷路了,我一直在尝试很多事情,当我尝试从asyncstorage读取本机响应时,我不明白为什么会收到此错误,我知道对于我来说,获取asyncstorage作为字符串我需要使用诸如parse之类的东西进行转换,但是我陷入了这种错误。

function webScraper($handle, $market, $url, $currentServerDate, $latestDate, $result){
    $fetch = $handle->query("SELECT `market` FROM `result` WHERE `market` = '$market' && `result_timestamp` >= CURRENT_DATE() AND `result_timestamp` < (CURRENT_DATE()+1)");  

    if(empty($handle->logError())){
        $row = $fetch->fetch();

        if($handle->rowCount($row) < 1){
            $scraper = new httpConnect($url);
            $scraper->setHeaders();
            $scraper->setOptions();
            $scraper->sendRequest();

            $info = $scraper->getInformations();
            $host = $info['url'];
            $ip_address = $info['primary_ip'];
            $http_code = $info['http_code'];

            if($info['http_code'] == 200){
                if($currentServerDate == $latestDate){
                    if($result !== NULL){
                        $error = $handle->query("INSERT INTO `result` (`market`, `number`) VALUES ('$market', '$result')");

                        $error = $handle->logError();

                        if(!empty($error)){
                            while(!empty($error)){
                                $error = $handle->query("INSERT INTO `result` (`market`, `number`) VALUES ('$market', '$result')");

                                $error = $handle->logError();
                            }
                        }
                    }
                }else{
                    $handle->query("INSERT INTO `regex_match_log` (`host`, `type`) VALUES ('$host', 'date')");
                }
            }else{
                $handle->query("INSERT INTO `request_log` (`host`, `ip_address`, `http_code`)
                            VALUES ('$host', '$ip_address', '$http_code')");
            }
        }
    }

这是我使用expo在设备上运行应用程序时遇到的错误

    class AddScreen extends React.Component {

      constructor(props) {
        i=0;
        super(props);
        var text = {};
        this.state = {text: ''};
      }

      setName = (text) => {
        text.name = text;
        //  alert(t)
        var name = ("name"+i++);
        //    alert(name);
        AsyncStorage.setItem(name, text);

        this.setState({ name: text });
        var booga=  AsyncStorage.getItem(name);
        // var stringified = JSON.stringify(booga.name);
        var booga1 = JSON.parse(booga);
        alert(booga1);
      }

      render() {
        return (
          <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>

          <TextInput
          style={{height: 100}}
          placeholder="Name of expense"
          onChangeText={this.setName}

          />
          <View style={{width: 50, height: 100}} />
          <Text>Add  Screen</Text>
          </View>
        );
      }


    }

1 个答案:

答案 0 :(得分:0)

为什么每次字符更改都调用异步存储功能? 这不是正确的方法。请阅读componentDidMound()函数上的异步存储。然后单击按钮或其他操作即可写入异步存储。

//method to read from async storage
readData() {
   AsyncStorage.getItem("LOGED_CREDENTIALS").then(value => {
      if (value) {
        let data = JSON.parse(value);
        // your code here...
      } else {
        //if error...
      }
   });
}

//write data
setData(data){
  AsyncStorage.setItem(
     "LOGED_CREDENTIALS",
     JSON.stringify(data)
  );
}