简体   繁体   English

错误未捕获(承诺中)TypeError:无法读取未定义的属性“记录集”

[英]error Uncaught (in promise) TypeError: Cannot read property 'recordset' of undefined

Im trying to use my api created in node witn the axios library and hook useEffect to display current item users and I'm getting error Uncaught (in promise) TypeError: Cannot read property 'recordset' of undefined, whY?我试图使用我在节点中创建的 api 和 axios 库并挂钩 useEffect 来显示当前项目用户,我收到错误 Uncaught (in promise) TypeError: Cannot read property 'recordset' of undefined,whY? API 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);
    }
});

Data数据

{
    "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
    ]
}

Function to call my API created in node js with react hooks useEffect and axios library Function 调用我在节点 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;

Please try changing this line:请尝试更改此行:

setuser(response.user);

to,至,

setuser(response);

Looks like your response data doesn't have a object with key user .看起来您的响应数据没有带有 key user的 object 。

It was to use the data property of axios as mentioned by the partner就是使用伙伴提到的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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拒绝(TypeError):无法读取未定义的属性“setState”,未捕获(在承诺中)类型错误:无法读取未定义的属性“setState” - Rejection (TypeError): Cannot read property 'setState' of undefined, Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined 我在 axios 补丁 api 中收到“Uncaught (in promise) TypeError: Cannot read property 'data' of undefined”错误 - I'm getting “Uncaught (in promise) TypeError: Cannot read property 'data' of undefined” error in axios patch api 错误:未捕获的TypeError:无法读取未定义的属性&#39;groupBy&#39; - Error:Uncaught TypeError: Cannot read property 'groupBy' of undefined Nodejs Promise TypeError:无法读取未定义的属性“then” - Nodejs Promise TypeError: Cannot read property 'then' of undefined 节点Promise-TypeError无法读取未定义的属性.then - Node Promise - TypeError cannot read property .then of undefined TypeError:无法读取未定义的Dialogflow Promise的属性&#39;then&#39; - TypeError: Cannot read property 'then' of undefined Dialogflow Promise Uncaught TypeError:无法读取未定义的属性“ prototype” - Uncaught TypeError: Cannot read property 'prototype' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;isElementPresent&#39; - Uncaught TypeError: Cannot read property 'isElementPresent' of undefined 未捕获的类型错误:无法读取未定义的属性“总计” - Uncaught TypeError: Cannot read property 'total' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;combineReducers&#39; - Uncaught TypeError: Cannot read property 'combineReducers' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM