简体   繁体   English

使用 ReactJs 和节点从 mongoDb 获取数据

[英]Fetch Data from mongoDb using ReactJs and node

I have a mern project where i want to fetch data from mongodb in my reactjs.我有一个 mern 项目,我想在我的 reactjs 中从 mongodb 获取数据。

I have gone through sevral previous question but it didn't help!我已经解决了几个以前的问题,但没有帮助!

i have successfully fetched all data all together我已经成功地获取了所有数据

But i want to fetch olny specific data ilke email .但我想获取 olny 特定数据 ilke email How can i do that?我怎样才能做到这一点?

Admin.js(frontend) Admin.js(前端)


import React from 'react';
import axios from 'axios';

class Admin extends React.Component {
  state = {
    phone: '',
    email: '',
    posts: []
  };

  // componentDidMount = () => {
  //   this.getBlogPost();
  // };

  hello = () => {
    axios.get('/getuser')
      .then((response) => {
        const data = response.data;
        this.setState({ posts: data });
        console.log(data);
      })
      .catch(() => {
        alert('Error retrieving data!!!');
      });
  }

  displayBlogPost = (posts) => {

    if (!posts.length) return null;

    return posts.map((post, index) => (
      <div key={index} className="blog-post__display">
        <h3>{post.phone}</h3>
        <p>{post.email}</p>
      </div>
    ));
  };


  render() {
    return (
      <>
        <h1>
          <button onClick={this.hello} class="btn btn-danger">click here to get data</button>
        </h1>

        <div className="blog-">
          {this.displayBlogPost(this.state.posts)}
          <table>
            <tr>
              <th>email</th>
              <th>phone</th>
            </tr>
            <tr>
              <td>rah098755@gmail.com</td>
              <td>8340251638</td>
            </tr>
            <tr>
              <td>kumar_rahulkkcs@yahoo.com</td>
              <td>78750251638</td>
            </tr>
            <tr>
              <td>anita@gmail.com</td>
              <td>9652251638</td>
            </tr>
          </table>
        </div>       
      </>
    )
  }
}


export default Admin;

i am getting data in my browser like this---我正在像这样在浏览器中获取数据---

我的浏览器的图像

How can i show olny specific data我如何显示 olny 特定数据

thankyou谢谢你

So I am going to assume you have an Express App running with endpoints you are calling from this front-end code given the /get-user endpoint.因此,我将假设您有一个 Express 应用程序运行,其中包含您从该前端代码调用的端点,并给出了/get-user端点。 The endpoint returns all of the user data for one or more users depending on how you set it up.端点返回一个或多个用户的所有用户数据,具体取决于您的设置方式。 In order to get a list emails for users, you will need something to distinguish your API call to the Express App that you want only the emails for the users, and not the full user object.为了获得用户的电子邮件列表,您需要一些东西来区分对 Express 应用程序的 API 调用,您只需要用户的电子邮件,而不是完整的用户对象。 There are a lot of ways to make this distinction, but another route is probably the best option to adhere to REST in some way.有很多方法可以进行这种区分,但另一种方法可能是以某种方式坚持 REST 的最佳选择。 Below is a possible example:下面是一个可能的例子:

Express Endpoint
app.get('/users/email', (req, res) => { 
   Call MongoDB and filter the user list to just email for response 
}

REACT Call
emails = () => {
    axios.get('/users/email')
      .then((response) => {
        const data = response.data;
        this.setState({ emails: data });
        console.log(data);
      })
      .catch(() => {
        alert('Error retrieving data!!!');
      });
  }

It is in the Express app that you should filter and ideally sort data for the front-end app to consume.您应该在 Express 应用程序中过滤并理想地对前端应用程序使用的数据进行排序。 Otherwise you risk sending too much data to the front-end or sending sensitive data by accident.否则,您可能会向前端发送过多数据或意外发送敏感数据。 Take a look at the below link for a bit more robust example.查看下面的链接以获得更强大的示例。

https://codingthesmartway.com/the-mern-stack-tutorial-building-a-react-crud-application-from-start-to-finish-part-1/ https://codingthesmartway.com/the-mern-stack-tutorial-building-a-react-crud-application-from-start-to-finish-part-1/

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

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