React Native-异常'-[NSNull长度]-执行函数后导航至页面时出错-

时间:2019-01-01 16:06:26

标签: javascript reactjs react-native asynchronous asyncstorage

我是本机反应的新手,我的应用程序有问题,希望您能帮助我解决。
在将inpunt(评论)发送给函数后,我再次导航至“搜索”页面 而不是返回到上一个“玩家”页面(因此将再次调用AsyncStorage.setItem的“ setAllComments”函数,
我收到此错误:

  

异常'-[NSNull长度]:无法识别的选择器已发送到实例   在目标上调用multiSet时引发了0x1cf86d9d0'   带参数的AsyncLocalStorage((((“ @ Ye-Music:songs”,“ null”

只有在我发送评论后才会发生

这是过程- gif process

导致应用崩溃的错误:

enter image description here

功能-

 async componentDidMount() {
  
    this.setAllComments();
  }

 
  setAllComments = async () => {
    console.log("setAllComments function!");
    fetch(URL + "/CommentsList2", {
      body: null,
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-type": "application/json; charset=UTF-8"
      }
    })
      .then(res => {
        return res.json();
      })
      .then(commentsResult => {
       AsyncStorage.setItem("@Ye-Music:comments", commentsResult.d);
      })
      .catch(err => {
        console.error(err);
      })
      .then(() => this.getComments());
  };



  async getComments() {
    await AsyncStorage.getItem("@Ye-Music:comments").then(value => {
      let commentsList = JSON.parse(value);
      this.setState({ comments: commentsList });
    });
  }

 

  addComment = async () => {
    await this.getUser();
    if (this.state.haveUser) {
      if (this.state.userText == "") {
        Alert.alert("comment can't be empty");
        return;
      } else if (this.state.userText.length <= 2) {
        Alert.alert("Comment must be longer than 2 chars");
        return;
      }
      this.setState({ haveUser: false });
      this.setState({ userText: this.state.userText.replace(/["]/g, "'") });

      let data = {
        userId: this.state.userData.ID,
        songId: this.state.song.Song_ID,
        username: this.state.userData.UserName,
        freeText: this.state.userText
      };
      fetch(URL + "/AddComment", {
        body: JSON.stringify(data),
        method: "POST",
        headers: {
          "Content-type": "application/json; charset=UTF-8"
        }
      })
        .then(res => {
          return res.json();
        })
        .then(commentData => {
          AsyncStorage.setItem("@Ye-Music:comment", commentData.d)
          .catch(err => {
            console.error(err);
          });
          let newCommentNow = AsyncStorage.getItem("@Ye-Music:comment").then(()=>
          {     let newUserComment = JSON.parse(newCommentNow);
            this.setState({ newComment: newUserComment });})
     
        })

        .then(this.setState({ writeComment: false, userText: "" }))
        .then(this.setAllComments())
        .then(
          Alert.alert("Re-open the song to see your comment", "", [
            { text: "Ok", onPress: () => null }
          ])
        );
    }


谁能帮助我解决这个问题。

0 个答案:

没有答案