繁体   English   中英

从后端获取数据时出错

[英]Getting Error when fetching data from backend

错误:XMLHttpRequest.handleError (xhr.js:84) 处的 createError (createError.js:16) 网络错误

当我尝试从我创建的后端获取数据时出现上述错误。

我在我的行动中写了下面的代码,

import axios from 'axios'
import {GET_PEOPLE} from './types'
export const startGetPeople = () => async(dispatch) => {
try {
    const res = await axios.get('http://localhost:5000/api/people')
    dispatch({
        type: GET_PEOPLE,
        payload: res.data
    })
} catch (error) {
    console.log("error from getpeopleAction",error)

我能够从 Postman 中的 API 获取数据。

Postman 响应:-

[
{
    "_id": "60a8b70be6384c8052fcc2e0",
    "name": "chan",
    "company": "micro",
    "profile": "uploads\\old.png",
    "__v": 0
},
{
    "_id": "60a8b80fe6384c8052fcc2e1",
    "name": "tucker",
    "company": "honda",
    "profile": "uploads\\employee.png",
    "__v": 0
},
{
    "_id": "60a8b84be6384c8052fcc2e2",
    "name": "emma",
    "company": "techsoft",
    "profile": "uploads\\girl.png",
    "__v": 0
},
{
    "_id": "60a8c004e6384c8052fcc2e3",
    "name": "amy",
    "company": "tec in",
    "profile": "uploads\\lady.jpg",
    "__v": 0
},
{
    "_id": "60a8d07627915c1e2429a8a2",
    "name": "tom",
    "company": "mil tech",
    "profile": "uploads\\tom-hardy.jpg",
    "__v": 0
}]

动作类型文件:-

导出常量 GET_PEOPLE = 'GET_PEOPLE'

试试下面的这个块:

async startGetPeople() {
  let response = () => {
   return new Promise(function(resolve, reject) {
    fetch('http://localhost:5000/api/people').then(response => {
      resolve(response);
    });
  });
 };
 let result = await response();
 return result.data
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM