error Uncaught (in promise) TypeError: Cannot read property 'recordset' of undefined
error Uncaught (in promise) TypeError: Cannot read property 'recordset' of undefined
我正在尝试使用我在节点中创建的 api 和 axios 库并挂钩 useEffect 来显示当前项目用户,但我收到错误 Uncaught (in promise) TypeError: Cannot read 属性 'recordset' 的未定义,为什么?
API
router.get('/api/users, async(req,res)=>{
try {
let results=await m.all();
res.json(results)
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
数据
{
"recordset": [
[
{
"id":1,
"Name":"Alissa",
"Age": 45,
"Adress": "Brooklyn 405",
"Phone": "212-324-4153"
},
{
"id":2,
"Name":"Ellis",
"Age": 23,
"Adress": "Boston 955",
"Phone": "212-324-4152"
}
],
"output": {},
"rowsAffected": [
2
]
}
调用我在节点 js 中创建的 API 的函数,带有反应挂钩 useEffect 和 axios 库
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
function AllUsers(){
const url='/api/users';
const[user, setuser]= useState({recordset: [] });
useEffect(()=>{
const getUsers=async()=>{
const response =await axios.get(url);
setuser(response.user);
}
getUsers();
},[]);
return(
<ul>
{user.recordset.map(item=>(
<li key={item.id}>
<a href={item.url}>{item.Name}</a>
</li>
))}
</ul>
)
}
export default AllUsers;
请尝试更改此行:
setuser(response.user);
到,
setuser(response);
您的响应数据似乎没有带键 user
的对象。
是用小伙伴说的axios的数据属性
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
function AllUsers(){
const url='/api/users';
const[user, setuser]= useState({recordset: [] });
useEffect(()=>{
const getUsers=async()=>{
const response =await axios.get(url);
**setuser(response.data);**
}
getUsers();
},[]);
return(
<ul>
{user.recordset.map(item=>(
<li key={item.id}>
<a href={item.url}>{item.Name}</a>
</li>
))}
</ul>
)
}
export default AllUsers;
我正在尝试使用我在节点中创建的 api 和 axios 库并挂钩 useEffect 来显示当前项目用户,但我收到错误 Uncaught (in promise) TypeError: Cannot read 属性 'recordset' 的未定义,为什么? API
router.get('/api/users, async(req,res)=>{
try {
let results=await m.all();
res.json(results)
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
数据
{
"recordset": [
[
{
"id":1,
"Name":"Alissa",
"Age": 45,
"Adress": "Brooklyn 405",
"Phone": "212-324-4153"
},
{
"id":2,
"Name":"Ellis",
"Age": 23,
"Adress": "Boston 955",
"Phone": "212-324-4152"
}
],
"output": {},
"rowsAffected": [
2
]
}
调用我在节点 js 中创建的 API 的函数,带有反应挂钩 useEffect 和 axios 库
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
function AllUsers(){
const url='/api/users';
const[user, setuser]= useState({recordset: [] });
useEffect(()=>{
const getUsers=async()=>{
const response =await axios.get(url);
setuser(response.user);
}
getUsers();
},[]);
return(
<ul>
{user.recordset.map(item=>(
<li key={item.id}>
<a href={item.url}>{item.Name}</a>
</li>
))}
</ul>
)
}
export default AllUsers;
请尝试更改此行:
setuser(response.user);
到,
setuser(response);
您的响应数据似乎没有带键 user
的对象。
是用小伙伴说的axios的数据属性
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { Link } from 'react-router-dom';
function AllUsers(){
const url='/api/users';
const[user, setuser]= useState({recordset: [] });
useEffect(()=>{
const getUsers=async()=>{
const response =await axios.get(url);
**setuser(response.data);**
}
getUsers();
},[]);
return(
<ul>
{user.recordset.map(item=>(
<li key={item.id}>
<a href={item.url}>{item.Name}</a>
</li>
))}
</ul>
)
}
export default AllUsers;