繁体   English   中英

如何限制用户每个项目只能发表一个评论和评分?

[英]how to limit users to only one comment and rating per Item?

我在后端控制器中就是这样:'use strict';

 var   Comment = require('../../../models/comment');

module.exports = {
    description: 'Create a Comment',
    notes: 'Create a comment',
    tags:['comment'],

    handler: function(request, reply){
        console.log('COMMM PAY', request.payload);
        Comment.create({
            itemId: request.payload.itemId,
            text: request.payload.commentText,
            rating: request.payload.rating,
            userId: request.auth.credentials._id
        }, function(err, comment){
            reply(comment);
        });
    }
};

这就是我在前端控制器中所拥有的:

$scope.createComment = function(comment, item, rating){
                            var body = {itemId:item.itemId,
                                commentText: comment.text,
                                rating: rating};

                        Comment.create(body).then(function(res){
                            toastr.success('Review Submitted.');
                            console.log('RESdfdas.data',res.data);
                            $scope.comments.push(res.data);
                            $scope.showCommentForm = !!!$scope.showCommentForm;
                            $scope.comment = {};
                            getComments();
                        });
                        };

如何做到这一点,使用户对每个项目只能发表一个评论/评分? 我知道我需要一个if / else条件来说明是否已经有匹配的文档/评论对象具有匹配的userId && itemId然后返回错误?

不知道您是否需要查看我的html / jade。

有几种方法可以解决此问题。 如果使用数据库,我会有一个'事件'表(集合),每当有人评价它时,它会记录一个标识符给他们和他们评价的内容。 然后,如果他们已经有匹配的记录,您可以禁用评级/评论。

一种不那么绝对的方式是在localstorage中存储一些东西,然后检查......但是这些信息可能会被删除,让他们再次访问评价/评论。

Mongoose / mongo让这非常容易。 您需要做的就是在您的服务器端路由中创建注释。

Comment.findOne({userId : request.auth.credentials._id}, function(err,data) {
  if (err) {
    // If err (doesn't find a comment made by that user)
    // Then create one
  } else {
    // If it hits here, it means there was already a comment from that user
    // So kick them out or whatever
  }
}

有几种方法可以做到这一点,用它来指导你的思考。

暂无
暂无

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

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