Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 我能';无法从api获取数据以作出反应_Reactjs - Fatal编程技术网

Reactjs 我能';无法从api获取数据以作出反应

Reactjs 我能';无法从api获取数据以作出反应,reactjs,Reactjs,我在从api获取数据以作出反应时遇到问题,我认为存在逻辑错误,您能帮助我吗?我不认为我在后端的VisualStudio上有任何错误,因为它在Postman上运行得很好,但在VisualStudio代码中我有这个问题。 我正在使用.net core 5.0,下面的视频使用3.1。我的代码如下: .env文件: REACT_APP_API=http://localhost:53535/api/ REACT_APP_PHOTOPATH=http://localhost:53535/Photos/ .

我在从api获取数据以作出反应时遇到问题,我认为存在逻辑错误,您能帮助我吗?我不认为我在后端的VisualStudio上有任何错误,因为它在Postman上运行得很好,但在VisualStudio代码中我有这个问题。 我正在使用.net core 5.0,下面的视频使用3.1。我的代码如下: .env文件:

REACT_APP_API=http://localhost:53535/api/
REACT_APP_PHOTOPATH=http://localhost:53535/Photos/
.Positions.js

import React,{Component} from 'react';
import {Table} from 'react-bootstrap';

import {Button,ButtonToolbar} from 'react-bootstrap';
import {AddPosModal} from './AddPosModal';

export class Positions extends Component{

    constructor(props){
        super(props);
        this.state={deps:[], addModalShow:false}
    }

    refreshList(){
        fetch(process.env.REACT_APP_API+'positions')
        .then(response=>response.json())
        .then(data=>{
            this.setState({deps:data});
        });
    }

    componentDidMount(){
        this.refreshList();
    }

    componentDidUpdate(){
        this.refreshList();
    }

    render(){
        const {deps}=this.state;
        let addModalClose=()=>this.setState({addModalShow:false});
        return(
            <div>
                <Table className="at-4" striped bordered hover size="sm">
                    <thead>
                        <tr>
                            <th>PositionID</th>
                            <th>Position</th>
                            <th>Options</th>
                        </tr>
                    </thead>
                    <tbody>
                        {deps.map(pos=>
                            <tr key={pos.PositionID}>
                                <td>{pos.PositionID}</td>
                                <td>{pos.Position}</td>
                                <td>Edit / Delete</td>
                            </tr>)}
                    </tbody>    
                </Table>

<ButtonToolbar>
    <Button variant='primary'
        onClick={()=>this.setState({addModalShow:true})}>
            Add Position
        </Button>

        <AddPosModal show={this.state.addModalShow}
        onHide={addModalClose}></AddPosModal>

</ButtonToolbar>

            </div>
        )
    }
}
import React,{Component}来自'React';
从'react bootstrap'导入{Table};
从“react bootstrap”导入{Button,ButtonToolbar};
从“./AddPosModal”导入{AddPosModal};
导出类位置扩展组件{
建造师(道具){
超级(道具);
this.state={deps:[],addModalShow:false}
}
刷新列表(){
获取(process.env.REACT\u APP\u API+“位置”)
.then(response=>response.json())
。然后(数据=>{
this.setState({deps:data});
});
}
componentDidMount(){
这个.refreshList();
}
componentDidUpdate(){
这个.refreshList();
}
render(){
const{deps}=this.state;
让addModalClose=()=>this.setState({addModalShow:false});
返回(
位置ID
位置
选择权
{deps.map(位置=>
{pos.PositionID}
{pos.Position}
编辑/删除
)}
this.setState({addModalShow:true})}>
添加位置
)
}
}

处理不同api端点时,以下函数中存在潜在错误。正如评论员所解释的,这不是导致此错误的原因,请参阅这是一个干净的代码最佳实践,以防以后必须处理不同的端点:) 职能:

refreshList(){
    fetch(process.env.REACT_APP_API+'positions')
“位置”是一个静态字符串,而不是字符串类型的动态变量

它应该是这样的(在Typescript中):


这怎么会是错误?它会导致
http://localhost:53535/api/positions/
,这正是OP想要获取的内容。他只是将文本字符串
位置
连接到基本api地址。你完全正确!我的回答并不能解决问题。我只是认为这是处理不同en时的一个潜在错误源D点并认为这可能是错误。我将编辑我的答案。谢谢!我正在观看此视频,在47:07分钟时我没有获得任何数据。我不知道问题出在哪里。那么,到底是什么问题?在这种状态下,由于缺少调试细节,您的问题将被关闭。我正在观看此视频,在47:07分钟时我有没有任何数据。我不知道问题出在哪里不提供任何调试细节就无法帮助您。不确定您所说的“…47:07分钟我没有任何数据”是什么意思。请具体说明,不要强迫人们观看部分视频,只是为了了解您确切问题的背景,并找出如何解决您神秘的问题描述。我正在使用api.net core、postman、sql作为后端,一切正常。我的localhost api是:5000 in postman。in react for front at.env文件他编写了REACT_APP_API“”。后端API url的端口是5000。我不知道哪条路径正确,两条路径都不适用me@zhulien我是否需要在我的代码中导入任何其他内容,并且我正在使用.net core 5.0和我下面使用的视频3.1。在获取数据方面有什么区别吗?
refreshList(thePosition: string){
    fetch(process.env.REACT_APP_API + thePosition)