首页 > 解决方案 > 处理与 react/next api 调用的不一致

问题描述

我有以下代码:

import React from 'react'
import { getUser } from '../services/UserService'

const HomeForm = ({username}) => {
    async function determineCounters() {      
        var user_data = await getUser(username);
        console.log(user_data);
    }
}

获取用户:

export async function getUser(data) {

  const username = data
  const response = await fetch(`http://localhost:4000/users/${username}`, {
      method: 'GET',
      headers: {'Content-Type': 'application/json'}
  })
  var response_data = await response.json();
  
  return(response_data);
}

该代码按预期工作,但在开发人员控制台中它首先显示错误,然后显示正确的输出。我相信这是因为承诺,但我不确定如何解决它。

安慰

标签: reactjsgoogle-chromeconsole

解决方案


推荐阅读