[英]How can I get comments to display under All posts
现在我有一个应用程序,我想从帖子中检索评论。 我不知道 go 的最佳方法是什么。
我目前只显示有评论的帖子,但它不会只显示一条评论。
<Grid item xl={8}>
{this.props.posts.map((post, index) =>
<PostBodyTemplate key={index} postId={post.id} onSubmit={this.handleSubmit} onChange={e => this.handleInputChange(e,post.id,post.userId,post.userIdName)} title={post.title} postBody={post.postBody}
giphyUrl = {post.giphyUrl} userWhoPosted={post.userIdName} commentBody={post.commentBody} userIdName={post.userIdName} />
)}
{displayGifPicker ? (<AddGif selectedGif = {this.getGifState} />) : (<Button size="small" onClick={this.displayGifPicker} ><button>Add Gif</button></Button>)}
</Grid>
数据来自 API,它基本上结合了 Posts 和 Comments 表,通过 PostId 连接它们,现在它只显示一条评论,并且当前只显示有评论的 Post。
router.get('/', (req, res) =>{
sequelize.query( "SELECT comments.id, comments.postId,
comments.commentBody, comments.giphyurl,
comments.postPicture,comments.userId, comments.userIdto,
comments.userIdName, comments.userIdtoName, posts.postBody, posts.title,
posts.giphyUrl, posts.postPicture, posts.userId, posts.userIdName,
posts.userIdto, posts.userIdtoName FROM comments INNER JOIN posts ON
comments.postId = posts.id;")
.then(([results, metadata]) => {
res.json(results)
})
})
有没有更好的方法我应该 go 关于这个? 我的问题的理想解决方案是抓取所有帖子,显示所有帖子,如果帖子有评论,那么也显示所有这些评论。 我的查询只返回有评论的帖子,通过它们映射只显示一条评论,它不显示全部。
要获取当前的所有评论,我只使用一个简单的 findAll,但这不会显示评论,因为评论在评论表中。 这就是我在上一个查询中加入表的原因。
如果有帮助,这就是道具被发送到的组件。
export default function PostBodyTemplate(props) {
const { onChange, onSubmit} = props
const classes = useStyles();
// render() {
return (
<Grid item xs={12} xl={8} lg={8} style={fr}>
<Card className={classes.card}>
<CardContent>
<Paper className={classes.root}>
<Typography variant="h5" component="h2" style={fr}>
{props.userWhoPosted} Gave A VH5 To Julio {props.postId}
</Typography>
<Typography variant="h5" component="h3">
{props.title}
</Typography>
<Typography component="p">
{props.postBody}
</Typography>
<img src={props.giphyUrl} style={giphyRes}/>
</Paper>
</CardContent>
<CardActions>
<IconButton aria-label="add to favorites">
<FavoriteIcon />
<div>Add Gif</div>
</IconButton>
<IconButton aria-label="share">
<EcoIcon />
<div>Add Photo</div>
</IconButton>
<form onSubmit={onSubmit}>
<div className={classes.container}>
<TextField
onChange = {onChange}
name='commentBody'
id="standard-full-width"
label="Reply To Post"
style={{ margin: 8 }}
placeholder="Reply to Post"
fullWidth
margin="normal"
InputLabelProps={{
shrink: true,
}}
/>
{/* <p><button>Send VH5</button></p> */}
{/* <Button onSubmit={onSubmit} size="small">Submit</Button> */}
<button onSubmit={onSubmit}>Submit VH5</button>
{/* <button onSubmit={onSubmit}>Submit Reply</button> */}
</div>
</form>
{/* <CommentInput onChange={onChange}/> */}
{/* <Button size="small">Submit</Button> */}
</CardActions>
<Paper className={classes.root} value={props.postId}>
<Typography variant="h5" component="h3">
{props.commentBody}
</Typography>
<Typography component="p">
{props.userIdName} replied to the post.
</Typography>
</Paper>
</Card>
</Grid>
)
// }
}
您需要将您的内部联接更改为LEFT JOIN
以获取所有帖子,无论他们是否有评论。 有关联接的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.