reactjs - 处理与 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);
}
该代码按预期工作,但在开发人员控制台中它首先显示错误,然后显示正确的输出。我相信这是因为承诺,但我不确定如何解决它。
解决方案
推荐阅读
- python - 想为大学项目构建自动订票应用程序
- python-3.x - 未调用 Python 属性设置器
- c - 包括用于 Xtensa Lx106 的标准库
- database - 我可以以哪种格式存储golang的时间。在scylladb中的时间?
- javascript - JSONArray 到 JavaScript 中的逗号分隔字符串
- node.js - 计算 Node.js Socket.IO 应用程序中的连接用户数
- php - WooCommerce:在挂钩函数中获取产品变体自定义字段值
- r - 在 R 中映射南极洲
- c - 为什么 Visual Studio Code 无法编译我的 C 代码?
- node.js - 我想返回帖子数组,但我得到了一个空数组