简体   繁体   English

无法正确将评论推入猫鼬数组

[英]Can't properly push comments to an array in mongoose

So I am building this app that allows users to post comments. 因此,我正在构建一个允许用户发表评论的应用程序。

I am running into a problem where when I try and post a comment, it doesn't actually add on to the already existing comment it just replaces the comment that was already there. 我遇到一个问题,当我尝试发布评论时,它实际上并没有添加到已经存在的评论中,而只是替换了已经存在的评论。 I am not sure what is going wrong to make this happen. 我不确定发生这种情况的原因是什么。

Here is my Express route for the comment: 这是我的快速发表评论的路线:

.put((req, res) => {
        Issue.findByIdAndUpdate(req.params.id, req.body, {$push: {comments: req.body.comments}}, (err, updatedComment) => {
            if(err) return res.status(500).send(err);
            return res.send(updatedComment);
        })
    })

Here is my Redux where I am actually using the same action creater for when I am editing the post to add comments: 这是我的Redux,在我编辑帖子以添加评论时,我实际上在使用相同的动作创建器:

export const getIssues = () => {
    return dispatch => {
        axios.get("/issues").then(response => {
            dispatch({
                type: "GET_ISSUES",
                issues: response.data
            })
        }).catch(err => {
            console.log(err);
        })
    }
}
export const editIssue = (editedIssue, id) => {
    return dispatch => {
        axios.put(`/issues/${id}`, editedIssue).then(response => {
            dispatch(getIssues());
        }).catch(err => {
            console.log(err);
        })
    }
}

const reducer = (state = [], action) => {
    switch(action.type){
        case "GET_ISSUES":
            return action.issues
        default:
            return state
    }
}

Here's the form to add a comment and the onClick method: 这是添加注释和onClick方法的表单:

addComment = () => {
        this.props.editIssue({
            comments: this.state.comment
        }, this.props.id)
    }
<button onClick={this.toggleComment}>Add A Comment</button>
                {this.state.isCommenting ? <form>
                    <input type="text" value={this.state.comment} name="comment" placeholder="Add A Comment..." onChange={this.handleChange}/>
                    <button onClick={this.addComment}>Submit</button>
                </form>: null}

And finally my Schema for the post: 最后是我的帖子架构:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const issuesSchema = new Schema({
    issue: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true,
    },
    comments: [String]
})

module.exports = mongoose.model("Issues", issuesSchema);

Let me know if there is anything else from my code you would want to see. 让我知道您想查看的代码中是否还有其他内容。 I purposefully omitted some of the code because I feel like this post is already long as is. 我故意省略了一些代码,因为我觉得这篇文章已经很长了。

your update function will be as below, (you dont have to give req.body as argument because of it, it is updating the existing one) 您的更新功能将如下所示((因为它,您不必给req.body作为参数,它会更新现有的)

.put((req, res) => {
        Issue.findByIdAndUpdate(req.params.id, {$push: {comments: req.body.comments}}, (err, updatedComment) => {
            if(err) return res.status(500).send(err);
            return res.send(updatedComment);
        })
    })

for more about findByIdAndUpdate 有关findByIdAndUpdate的更多信息

or try this below 或在下面尝试

.put((req, res) => {
    Issue.update({
        _id: req.params.id
    },
        {
            $push: {
                comments: req.body.comments
            }
        }, (err, updatedComment) => {
            if (err) return res.status(500).send(err);
            return res.send(updatedComment);
        })
})

for more about update for more about $push 有关更新的更多信息有关$ push的更多信息

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

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