对异步的误解也许是?控制台日志未在.then()

时间:2018-09-25 16:46:47

标签: reactjs

我有一个React应用,在该应用中,我有一个componentdidmount函数,看起来像这样:(已编辑的非必要信息)(在底部的说明)

import '../css/options2.css'
import React from 'react'
import ReactDOM from 'react-dom'

import { Auth } from 'aws-amplify'

import SearchSaved from './options2Components/SearchSaved.js'
import SavedList from './options2Components/SavedList.js'

import  { configureStore }  from './store';
import { addToSaved } from './actions/saved-items.js';

const store = configureStore();


export default class Options2 extends React.Component {
     componentDidMount() {

       //Here I want to grab the user session and id with an async funct

       Auth.currentSession()
      .then((data) => {

       //I attempt to log data in order to check values at given a given time

        console.log(data, "data")
        let user =  data.username
        return user
        console.log(user)
        fetch("fetchToAPI/get", {
          credentials: 'same-origin',
          method: 'GET',
          headers: { 'Content-Type': 'application/json', 'user': user }
        })
        .then(response => response.json())
        .catch(err => console.log(err))
        .then(responseData => {

         // if the data is acquired log it some more and map it out 

          console.log(responseData, "hi")
          var dataMapped = responseData.map(data => ({
            associatedMp3: data.associatedMp3,
            saveId: data.saveId,
            textValue: data.text,
            title: data.title,
            user: data.user,
            voiceSelected: data.voiceSelected,
            createdAt: data.createdAt
          }));

          //  store.dispatch(addToSaved({dataMapped}))
          //  console.log(dataMapped)

        })
        .catch(err => console.log(err))
       })
       .catch(() => {
         console.log('not auth')
       })
  }

  // Redacted render call

没有任何控制台日志正在打印!我尝试以异步等待方式对其进行重新制作,但变得更加困惑。该文档非常清楚,但我仍然不知道为什么无法打印日志。好像错误日志会出现错误,但是成功后似乎什么也没发生。

一切都配置正确,这似乎只是我处理错误情况的方式。

如果有人可以解释两件事,那将很酷: 如何获取控制台日志进行打印 和 我的代码是否为我要实现的目标编写得不好?

如果您需要知道什么,我还是比较陌生,请告诉我!

编辑

仅从currentSession中获取控制台日志数据而没有任何额外请求的功能不会返回任何控制台日志语句

componentDidMount() {
       console.log("hello")
       //Here I want to grab the user session and id with an async funct

       Auth.currentSession()
      .then((data) => {

       //I attempt to log data in order to check values at given a given time

        console.log(data, "data")
        let userId =  data.username
        console.log(userId)
       })
       .catch((err) => {
         console.log(err)
       })
  }

0 个答案:

没有答案