简体   繁体   English

如何在 React 中正确绑定来自 MongoDB 的数据?

[英]How to properly Bind data from MongoDB in React?

Basically I'm using two components to get data from the DB.基本上我使用两个组件从数据库中获取数据。 One that I'm using to loop through them and a second one to access and view the data for each entry.我用来循环遍历它们的一个,另一个用于访问和查看每个条目的数据。 I have a field in DB called 'sex' in which the users submits either male, female and/or other.我在数据库中有一个名为“性别”的字段,用户可以在其中提交男性、女性和/或其他人。 I'm trying to do an if statement but it is not working or maybe I'm not that good as I though.我正在尝试做一个 if 语句,但它不起作用,或者我可能没有我那么好。

I have done this in the file(Secrets.js) in which I'm looping through(I'm using a different component within as well) them:我已经在我正在循环的文件(Secrets.js)中完成了这项工作(我也在其中使用了不同的组件)它们:

import React, { Fragment, useEffect } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Spinner from '../layout/Spinner';
import SecretItem from './SecretItem';
import { getSecrets } from '../../actions/secret';

const Secrets = ({ getSecrets, secret: { secrets, loading } }) => {
  useEffect(() => {
    getSecrets();
  }, [getSecrets]);
  return loading ? (
    <Spinner />
  ) : (
    <Fragment>
      <div className="container">
        <Fragment>
          {secrets.map(secret => (
            <SecretItem key={secret._id} secret={secret} />
          ))}
        </Fragment>
      </div>
    </Fragment>
  );
};

Secrets.propTypes = {
  getSecrets: PropTypes.func.isRequired,
  secret: PropTypes.object.isRequired
};

const mapStateToProps = state => ({
  secret: state.secret
});

export default connect(
  mapStateToProps,
  { getSecrets }
)(Secrets);

The problem it is not that I'm not able to fetch data because I actually can, I can get the title, date, text, etc. What the real problem is - is when wanting to fetch conditional data as said in the first paragraph.问题不在于我无法获取数据,因为我实际上可以,我可以获得标题、日期、文本等。真正的问题是 - 是当想要获取第一段中所述的条件数据时. Here it is my SecretItem component:这是我的 SecretItem 组件:

import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { deleteSecret } from '../../actions/secret';

const SecretItem = ({ secret: { _id, age, sex, text, date } }) => {
  if (sex === 'male') {
    const sexo = 'male';
  } else if (sex === 'female') {
    const sexo = 'female';
  } else {
    const sexo = 'other';
  }
  return (
    <article className={`card text-center ${_id}`}>
      <div className="card-header">
        <span className="badge age badge-info">{age}</span>
        <span className="badge sex badge-secondary">{sexo}</span>
        <p>{text}</p>
        <P>{date}</P>
      </div>
    </article>
  );
};

SecretItem.propTypes = {
  secret: PropTypes.object.isRequired,
  deleteSecret: PropTypes.func.isRequired
};

export default connect(
  null,
  { deleteSecret }
)(SecretItem);

So far it just keeps throwing me error of undefined or it simply do not display anything.到目前为止,它只是不断向我抛出未定义的错误,或者根本不显示任何内容。 Allow me to say this is my first time using React, I actually never had troubles with JavaScript so if any of you guys can guide and me and tell me what my error is, please do so.请允许我说这是我第一次使用 React,实际上我从来没有遇到过 JavaScript 的问题,所以如果你们中的任何人可以指导我并告诉我我的错误是什么,请这样做。

Like Li357 said your scope of sexo was unreachable.就像 Li357 说你的sexo范围是sexo

import React, { Fragment, useState } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { deleteSecret } from '../../actions/secret';

const SecretItem = ({ secret: { _id, age, sex, text, date } }) => {
  const sexo = sex || 'other';

  return (
    <article className={`card text-center ${_id}`}>
      <div className="card-header">
        <span className="badge age badge-info">{age}</span>
        <span className="badge sex badge-secondary">{sexo}</span>
        <p>{text}</p>
        <P>{date}</P>
      </div>
    </article>
  );
};

SecretItem.propTypes = {
  secret: PropTypes.object.isRequired,
  deleteSecret: PropTypes.func.isRequired
};

export default connect(
  null,
  { deleteSecret }
)(SecretItem);

If the value of sexo becomes more complex to set, you have to define the value as a let instead, before the conditional statements.如果设置 sexo 的值变得更复杂,则必须在条件语句之前将该值定义为 let 。 For example:例如:

let sexo = 'not specified' 

if (sex === 'male') {
  sexo = 'male';
} else if (sex === 'female') {
  sexo = 'female';
} else {
  sexo = 'other';
}

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

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