【问题标题】:View collections item in Mern在 Mern 中查看收藏项目
【发布时间】:2019-12-25 23:44:05
【问题描述】:

我在 mongodb 集合中有一些项目,现在我想在 react 应用程序上查看它们,我有那个代码,但是 它什么也不显示 ,但是如果我用 console.log() 检查 value 我会得到内容。我该怎么办?


import axios from "axios";

const viewMails = []

axios.get('http://localhost:5000/emails').then(res => {

    let emailString =  JSON.parse(res.request.response)

    for (const [index, value] of Array(emailString).entries()) {
        viewMails.push(
            <div key={index}>
                <h1>{value.name}</h1>
                <h3>{value.email}</h3>
                <p>{value.message}</p>
                <p>{value.createdAt}</p>
            </div>
        );
    }

});

export default class EmailsViewer extends Component {

  render() {
    return (
      <div className="emails">
            <h1>Sos</h1>
            {viewMails}
      </div>
    );
  }
}```

【问题讨论】:

    标签: node.js reactjs mongodb collections


    【解决方案1】:

    由于您正在尝试做一个简单的组件来显示列表,如果您使用的是最新版本的 React 之一,请考虑使用 axios 挂钩(查看包文档以了解如何将其添加到您的项目中 @987654321 @)

    在这里,我向您展示了您需要做什么的示例:list demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多