无法从“赞”部分中删除用户ID

时间:2019-06-25 09:17:42

标签: javascript node.js mongoose

我已经尝试了几乎所有的拼接,弹出,移动,删除操作,但是我无法删除用户ID,这很简单,这意味着对帖子进行投票。

这是我的代码:

// @type    POST
// @route   /api/question/upvote/:id
// @desc    route for upvoting answers to questions
// @access  PRIVATE

router.post('/upvote/:id', passport.authenticate('jwt', {session:false}), (req, res)=> {
    Profile.findOne({user: req.user.id})
        .then(profile => {
            Question.findById(req.params.id)
                .then(question => {
                    if(question.upvotes.filter(upvote => upvote.user.toString() === req.user.id.toString()).length > 0){
                        return res.status(400).json({noUpvote : 'User is downvoted the question'});
                    }
                    question.upvotes.unshift({user : req.user.id});
                    question.save()
                        .then(question => res.json(question))
                        .catch(err => console.log('Error on saving the upvote user id :' + err));
                })
                .catch(err => console.log('Error on getting the question : ' + err));
        })
        .catch(err => console.log('Error on finding the user : ' + err));
});

我的应用程序中有三种模型:

//Load the Person Model
const Person = require('../../models/Person');

//Load the Profile Model
const Profile = require('../../models/Profile');

//Load the Questions Model
const Question = require('../../models/Questions');

因此,投票是一个有问题的模型。 Person模型包含注册和登录信息。 Profile模型包含“人员”详细信息。 Question模型包含问题,答案,评论和投票。

这是问题模型:

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

const QuestionSchema = new Schema({

    user : {
        type: Schema.Types.ObjectId,
        ref: 'myPerson'
    },
    textone: {
        type: String,
        required: true
    },
    texttwo: {
        type: String,
        required: true
    },
    name:{
        type: String
    },
    upvotes: [{
        user : {
            type: Schema.Types.ObjectId,
            ref: 'myPerson'
        }
    }],
    answers:[{
        user : {
            type: Schema.Types.ObjectId,
            ref: 'myPerson'
        },
        answer: {
            type: String,
            required: true
        },
        name:{
            type: String
        },
        date: {
            type: Date,
            default: Date.now
        }
    }],
    comments: [{
        user : {
            type: Schema.Types.ObjectId,
            ref: 'myPerson'
        },
        name:{
            type: String
        },
        text: {
            type: String,
            required: true
        },
        date: {
            type: Date,
            default: Date.now
        }

    }],
    date: {
        type:Date,
        default: Date.now
    }


});

module.exports = Questions = mongoose.model("myQuestion", QuestionSchema);

1 个答案:

答案 0 :(得分:0)

您可以使用过滤器删除ID。

question.upvotes = question.upvotes.filter(upvote => upvote.user.toString() !== req.user.id.toString());
question.save();

将其添加到if条件中(如果要删除的话)。