제출 처리기 날개로 연기 드리프트 같이하지만 반응시과 같은 처리

0

질문

내가 노력하고 웹을 만드는 업로드 파일 첨부된 현재 사용자는 파일한 모델로 외국인 핵심이다. 이는 get 요청이 초기화 되지만,그것은 처음에 필요한 정보입니다.

  handleSubmit = (e) => {
    e.preventDefault();
    axios.get('http://127.0.0.1:8000/core/current_user/', {
      headers: {
        Authorization: `JWT ${localStorage.getItem('token')}`,
      }
    }).then((user) => {

      this.state.creator = user.data;
      console.log(this.state.creator);
    })  
    console.log(this.state.creator);
    let form_data = new FormData();
    form_data.append('creator', this.state.creator);
    form_data.append('file', this.state.file);
    form_data.append('title', this.state.title);
    form_data.append('description', this.state.description);
    axios.post('http://localhost:8000/core/posts/', form_data, {
      headers: {
        'Content-Type': 'multipart/form-data',
        Authorization: `JWT ${localStorage.getItem('token')}`,
      }
    }).then(res => {
        console.log(res.data);
      }).catch(err => console.log(err))
  };

1 호 콘솔은 사용자 정보 그러나 2 차 콘솔 null 을 반환합니다. 어떤 도움이 될 것입니다 정말 감사합니다.

api axios javascript react-native
2021-11-23 22:41:32
1

최고의 응답

1

귀하의 then 문 후 원 get 끝에 선 11,및 나머지 부분의 코드입니다.

비동기 코드는,외부에서 코드의 then 블록 계속 실행되는 동안 그것의 응답을 기다리고 있는,그래서 this.state.creator 이 없었다는 아직 설정. 다음으로 돌아갑니다 내 코드 then 단한 약속을 확인합니다.

필요하신 모든 이동의 두번째 블록의 코드가 내부에 intial then 블록을 그래서 그것은 단지 한 번 실행되면 응답을 원 get 요청을 돌아왔다:

handleSubmit = (e) => {
  e.preventDefault();
  axios
    .get('http://127.0.0.1:8000/core/current_user/', {
      headers: {
        Authorization: `JWT ${localStorage.getItem('token')}`,
      },
    })
    .then((user) => {
      this.state.creator = user.data;
      console.log(this.state.creator);
      let form_data = new FormData();
      form_data.append('creator', this.state.creator);
      form_data.append('file', this.state.file);
      form_data.append('title', this.state.title);
      form_data.append('description', this.state.description);
      axios
        .post('http://localhost:8000/core/posts/', form_data, {
          headers: {
            'Content-Type': 'multipart/form-data',
            Authorization: `JWT ${localStorage.getItem('token')}`,
          },
        })
        .then((res) => {
          console.log(res.data);
        })
        .catch((err) => console.log(err));
    });
};
2021-11-24 00:46:59

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................