首页 > 解决方案 > 变量显示未定义

问题描述

我正在点击一个 api 并将响应保存在一个变量中,但是我存储的变量显示为调试器未定义。

        try {
      let response = await fetch(
       "http://test.kelltontech.net/eventengine/getmyevents?hash=****&timestamp=****&id=****",
        {
          method: "GET",
          headers: {
            "Content-Type": "application/json"
          }
        }
      );
      let responseJson = await response.json();
      this.agendaDetails = await responseJson;
    } catch (error) {
      alert(error);
    }

这里 responseJson 有数据,但没有存储在主类之外声明的议程详细信息中,如议程详细信息= {}

标签: javascriptreact-native

解决方案


您应该使用 fetch 和 .then() 在 json 中获取响应,然后将该响应分配给变量,如下所示

fetch(URL.apiUrlUploadImages, {
                method: "POST",
                headers: {
                    Accept: 'application/json',
                    'Authorization': 'Bearer ' + this.token,
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    "front_image": this.frontImageData
                })
            })
                .then((response) => response.json())
                .then((responseData) => {
                     var result = responseData});

推荐阅读