我正在做一个项目,当来宾或登录用户单击其名称时,该项目要求显示特定用户的帖子,这会将用户的ID(任何人都希望看到的帖子)传递给URL参数。我试图从URL获取user_id并将其传递给函数,该函数返回用户发布的所有帖子。
我正在使用MERN堆栈,很抱歉提出这样一个愚蠢的问题,但是,我对这项技术不熟悉。
以下是获取帖子的途径:
CASE
WHEN {location} LIKE '%00' OR {location} LIKE '%010' OR {location} LIKE '%011' OR {location} LIKE '%012' OR {location} LIKE '%013' THEN '1'
WHEN {location} LIKE '%38' OR {location} LIKE '%039' OR {location} LIKE '%040' OR {location} LIKE '%041' OR {location} LIKE '%042' OR {location} LIKE '%043' OR {location} LIKE '%044' OR {location} LIKE '%046' OR {location} LIKE '%047' THEN '2'
ELSE '3'
END
以下是操作,当访问URL时会调用该操作:
// @route GET api/posts/posted/:id
// @desc Get posts by ID
// @access Public
router.get("posted/:user", (req, res) => {
Post.find({ user: req.users._id })
.sort({ date: -1 })
.then(posts => res.json(posts))
.catch(err =>
res.status(404).json({ nopostfound: "No post found with that ID" })
);
});
这是显示帖子的代码示例
// Get Posts by userId
export const getUserPosts = user => dispatch => {
dispatch(setPostLoading());
axios
.get(`/api/posts/posted/${user}`)
.then(res =>
dispatch({
type: GET_POSTS,
payload: res.data
})
)
.catch(err =>
dispatch({
type: GET_POSTS,
payload: null
})
);
};
这是PostFeed代码:
import React, { Component } from "react";
import PropTypes from "prop-types";
import { connect } from "react-redux";
import PostedFeed from "./PostedFeed";
import Spinner from "../common/Spinner";
import { getUserPosts } from "../../actions/postActions";
class MyPosts extends Component {
componentDidMount() {
this.props.getUserPosts(this.props.match.params);
}
render() {
const { posts, loading } = this.props.post;
let postedContent;
if (posts === null || loading) {
postedContent = <Spinner />;
} else {
postedContent = <PostedFeed posts={posts} />;
}
return (
<div className="marginContainer">
<div className="container">
<div className="row">
<div className="col-md-12">{postedContent}</div>
</div>
</div>
</div>
);
}
}
MyPosts.propTypes = {
getUserPosts: PropTypes.func.isRequired,
post: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
post: state.post
});
export default connect(
mapStateToProps,
{ getUserPosts }
)(MyPosts);
这是帖子的骨架,它通过PostFeed传递到MyPost:
import React, { Component } from "react";
import PropTypes from "prop-types";
import PostedItem from "./PostedItem";
class PostedFeed extends Component {
render() {
const { posts } = this.props;
return posts.map(post => <PostedItem key={post._id} post={post} />);
}
}
PostedFeed.propTypes = {
posts: PropTypes.array.isRequired
};
export default PostedFeed;
现在告诉我应该怎么做才能填充页面上的帖子?
答案 0 :(得分:0)
后退:
1)poster/:user
表示用户ID 在req.params.user
2)当没有帖子 给用户时,它将返回空数组:{{1} },因此您必须检查客户端的空度
3)如果出现问题,猫鼬错误会发生-空结果不会引发错误
修复此问题,然后尝试:
[]
前
1)我看不到,您正在向用户ID 传递信息,确定// @route GET api/posts/posted/:id
// @desc Get posts by ID
// @access Public
router.get("posted/:user", async (req, res) => {
try {
const posts = await Post.find({user: req.users._id}).sort({date: -1}).lean();
res.status(200).send(posts);
}
catch (error) {
res.status(500).send({error: error.message});
}
});
持有用户ID ?可能是:this.props.match.params
吗?
我在说这个代码块:
this.props.match.params.userId
因此,请检查您的路由器并找到属性名称。
2)修复了渲染方法,告知用户没有帖子,原因是class MyPosts extends Component {
componentDidMount() {
this.props.getUserPosts(this.props.match.params.userId);
}
...
}
是不正确的原因,因为我们更改了后端以返回空数组:
posts === null
答案 1 :(得分:0)
我认为您应该使用以下代码获取值:
const userId = req.params.user;
通常,MongoDB将文档的ID存储到“ _id”中。我只想让您确保用户ID存储在“用户”中。
示例通用代码应为:
const userId = req.params.user;
Post.find({ _id: userId })
.sort({ date: -1 })
.then(posts => {
res.status(200).json(posts);
})
.catch(err =>
res.status(404).json({ nopostfound: "No post found with that ID" })
);
我还建议将值保存在常量变量中并使用该变量。它使您的代码更具可读性。