Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.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
Javascript 反应+;NodeJs获取问题_Javascript_Node.js_Reactjs - Fatal编程技术网

Javascript 反应+;NodeJs获取问题

Javascript 反应+;NodeJs获取问题,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,我正在尝试获取api的以下结果(运行良好) 进入react组件(一个简单的数组) 从服务器.js中提取的端点代码: app.get('/api/continents', (req, res) => { connection.query(SELECT_ALL_CONTINENTS_QUERY, (err, results) => { if(err){ return res.send(err) } else

我正在尝试获取api的以下结果(运行良好)

进入react组件(一个简单的数组)

从服务器.js中提取的端点代码:

app.get('/api/continents', (req, res) => {
    connection.query(SELECT_ALL_CONTINENTS_QUERY, (err, results) => {
        if(err){
            return res.send(err)
        }
        else{
            return res.json({
                data: results
            })
        }
    });
});
这是我的大陆.js代码:

import React, { Component } from 'react';
import './continents.css';

class Continents extends Component {
    constructor() {
        super();
        this.state = {
            continents: []
        }
    }

    ComponentDidMount() {
        fetch('http://localhost:5000/api/continents')
            .then(res => res.json())
            .then(continents => this.setState({continents}, () => console.log('Continents fetched..', continents)));
    }

  render() {
    return (
      <div>
        <h2>Continents</h2>
      </div>
    );
  }
}

export default Continents;
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Continents from './components/continents/continents';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Developed with NodeJS + React</h1>
        </header>
        <Continents />
      </div>
    );
  }
}

export default App;
import React,{Component}来自'React';
导入“/contricons.css”;
类扩展组件{
构造函数(){
超级();
此.state={
大陆:[]
}
}
ComponentDidMount(){
取('http://localhost:5000/api/continents')
.then(res=>res.json())
.then(大陆=>this.setState({columents},()=>console.log('columents fetched..',大陆));
}
render(){
返回(
大洲
);
}
}
输出默认大陆;
下面是App.js的代码:

import React, { Component } from 'react';
import './continents.css';

class Continents extends Component {
    constructor() {
        super();
        this.state = {
            continents: []
        }
    }

    ComponentDidMount() {
        fetch('http://localhost:5000/api/continents')
            .then(res => res.json())
            .then(continents => this.setState({continents}, () => console.log('Continents fetched..', continents)));
    }

  render() {
    return (
      <div>
        <h2>Continents</h2>
      </div>
    );
  }
}

export default Continents;
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Continents from './components/continents/continents';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Developed with NodeJS + React</h1>
        </header>
        <Continents />
      </div>
    );
  }
}

export default App;
import React,{Component}来自'React';
从“/logo.svg”导入徽标;
导入“/App.css”;
从“./组件/大陆/大陆”导入大陆;
类应用程序扩展组件{
render(){
返回(
使用NodeJS+React开发
);
}
}
导出默认应用程序;
问题:


大陆数组为空。没有提取数据。但是,没有错误。如果有人能为我指明解决问题的正确方向,我将不胜感激。非常感谢。

@Razvan能否请您更正组件的名称安装方法。它应该是componentDidMount而不是componentDidMount。C必须是小写。

我认为从API服务调用返回的数据结构包含一个
数据
键。尝试将设置状态更改为:

this.setState({continents: continents.data})

如果这不能解决问题,我会仔细检查您传递的数据结构的形状,因为逻辑似乎正确。

ComponentDidMount
是一个函数,因此它不应该大写,应该是:
ComponentDidMount

如果您将不正确的值传递给
setState
方法,则应该传递
{columents:columents.data}
,而不是
columents
对象本身

更正后的代码:

componentDidMount() {
    fetch('http://localhost:5000/api/continents')
        .then(res => res.json())
        .then(continents => this.setState({continents: continents.data}));
}

fetch在某些浏览器上有问题。我们面临着三星原生浏览器所有iPhone ios11 safari浏览器的问题。。我们应用的修复程序正在添加凭据 获取(“/users”{ 凭据:“相同来源” })

以下抄袭自

•Firefox 39-60 •铬42-67 •Safari 10.1-11.1.2 如果您以这些浏览器为目标,建议始终在所有获取请求中明确指定凭据:“同一来源”,而不是依赖默认值: 获取(“/users”{ 凭据:“相同来源”
})

谢谢,componentDidMount()方法的拼写也有问题。