【发布时间】:2020-11-03 19:58:59
【问题描述】:
我做了一个教程,能够显示我所有的文章对象,但我不知道如何修改代码以获取一个特定对象并将其存储在状态中。我尝试了很多不同的方法,但我不断收到“TypeError: Cannot read property 'name' of undefined”。
请注意,我要查找的 id 存储在“this.props.match.params.id”中,但我真的不知道这意味着什么或如何使用它。谢谢
ArticleShow.js
import React, { Component } from "react";
import { Container } from "reactstrap";
import { connect } from "react-redux";
import { getArticle } from "../actions/articleActions";
import PropTypes from "prop-types";
class articleShow extends Component {
componentDidMount() {
this.props.getArticle();
}
render() {
const { article } = this.props.article;
return (
<Container>
{article.name}
<br />
{article.author}
<br />
{article.body.split("\r").map((c) => {
return <p> {c} </p>;
})}
<br />
</Container>
);
}
}
ArticleShow.propTypes = {
getArticle: PropTypes.func.isRequired,
article: PropTypes.object.isRequired,
};
const mapStateToProps = (state, props) => ({
article: state.article,
});
export default connect(mapStateToProps, { getArticle })(ArticleShow);
articleActions.js
import axios from "axios";
import {
GET_ARTICLES,
GET_ARTICLE,
} from "./types";
export const getArticles = () => (dispatch) => {
dispatch(setArticlesLoading());
axios.get("/api/articles").then((res) =>
dispatch({
type: GET_ARTICLES,
payload: res.data,
})
);
};
export const getArticle = (id) => (dispatch) => {
dispatch(setArticlesLoading());
axios.get(`/api/articles/${id}`).then((res) =>
dispatch({
type: GET_ARTICLE,
payload: res.data,
})
);
};
articleReducer.js
import {
GET_ARTICLES,
GET_ARTICLE,
} from "../actions/types";
const intialState = {
articles: [],
loading: false,
};
export default function (state = intialState, action) {
switch (action.type) {
case GET_ARTICLES:
return {
...state,
articles: action.payload,
loading: false,
};
case GET_ARTICLE:
return {
...state,
article: action.payload,
loading: false,
};
default:
return state;
}
}
routes/api/articles.js
const express = require("express");
const router = express.Router();
// Article Model
const Article = require("../../models/Article");
router.get("/", (req, res) => {
Article.find()
.sort({ date: -1 })
.then((articles) => res.json(articles));
});
router.get("/:id", (req, res) => {
Article.findById(req.params.id).then((article) => res.json(article));
});
module.exports = router;
models/Article.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// create schema
const ArticleSchema = new Schema({
name: {
type: String,
required: true,
},
author: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now,
},
});
module.exports = Article = mongoose.model("article", ArticleSchema);
store.js
import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers";
const initialState = {};
const middleware = [thunk];
const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleware),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
export default store;
【问题讨论】:
-
您会用文章数据更新文章节点吗?另外,在文章数组中,您能否分享一下您正在分享的代表性数据
-
这是所有 Articles 对象的 JSON,我试图通过其 ID 获取其中一个。 ``` {"_id":"5f0b628f172467147fbed0c2","name":"Article 4","author":"Carol Henderson","body":"orem Ipsum 就是 d,{"_id":"5f0b6208b4841b13d8f08363", "name":"Article 3","author":"Joe Smith","body":"orem Ipsum is"}] ``
标签: javascript reactjs redux react-redux