Answers for "like operator mangodb"

0

like operator mangodb

let like = req.body.like;
// if request is a like
if (like) {
    Card.update({
        '_id': mongoose.Types.ObjectId(card_id),
        'likedBy': { '$ne': mongoose.Types.ObjectId(user_id) }
    }, {
        '$inc': { 'likes': 1 },
        '$push': { 'likedBy': mongoose.Types.ObjectId(user_id) }
    }, function(err) {
        if (err) {
          console.log(err);
           return res.json({
            'state': false,
            'msg': err
           })
        }

        return res.json({
            'state': true,
            'msg': 'Liked',
        })
    })
} else if (!like) { // if request is dislike
    Card.update({
        '_id': mongoose.Types.ObjectId(card_id),
        'likedBy': mongoose.Types.ObjectId(user_id) 
    }, {
        '$inc': { 'likes': -1 },
        '$pull': { 'likedBy': mongoose.Types.ObjectId(user_id) }
    }, function(err,) {
        if (err) {
           console.log(err);
           return res.json({
            'state': false,
            'msg': err
           })
        }

        return res.json({
            'state': true,
            'msg': 'Disliked',
        })
    })
}
Posted by: Guest on October-15-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language