【问题标题】:Send data from server(Node.js) to client(React Component) in GraphQL server将数据从服务器(Node.js)发送到 GraphQL 服务器中的客户端(React 组件)
【发布时间】:2020-03-14 14:19:56
【问题描述】:

我在 GraphQL 服务器中使用过 mongoDB、react.js 和 nodejs。我只想从服务器中的 mongodb 获取数据并将其发送到 react.js 组件负责的客户端。 我该怎么做?

【问题讨论】:

标签: node.js reactjs mongodb graphql


【解决方案1】:

试试这个,你可以从 MongoDB 中获取数据。

例如:

componentDidMount() {
    fetch(URL) // URL:- pass your nodejs endpoint
      .then(results => results.json())
      .then(data => this.setState({ results: data }))
      .catch(error=>{   
          if(error.response){
              alert("Error"); 
          }
      });
  }

【讨论】:

  • 我应该把它放在react组件中吗?
  • 是的,在你的反应组件中。无论你有什么状态,你都可以用它来代替“结果”。
猜你喜欢
  • 1970-01-01
  • 2013-04-11
  • 2021-12-07
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多