用户每个主题只投票一次,但他们可以改变主意

时间:2016-09-07 07:59:18

标签: javascript arrays node.js mongodb express

我需要逻辑方面的帮助。我在我的网站上的评论中有“有用”和“非有用”按钮。我这样做是为了当用户点击它们时我得到一个像usefulness: { usefulResult: 1, notUseful: 0, useful: 1 },这样的对象,所以每当用户点击一个按钮时,相应的属性就会增加。而usefulResult只是useful - notUseful

我现在面临的主要问题是,我希望一个用户只能投一票进行审核,但他们可能会在以后改变主意。我知道如何找出哪个用户正在进行投票。但是我想不出逻辑,所以伯爵不会增加超过1票。

这很糟糕: 坏:用户可以继续点击notUseful按钮,notUseful按钮计数器不断上升。

信息:usefulness对象具有特定审阅的所有用户的计数,因此可以从先前的数据填充。所以我们假设数据原来是usefulness: { usefulResult:-2 , notUseful: 5, useful: 3},而新用户投票useful它应该是usefulness: { usefulResult:-1 , notUseful: 5, useful: 4},如果他随后改变了主意并且投票notUseful该对象应该更改为{ {1}}。

我和mongoose一起工作并表达。我试了很多东西。这是我正在使用的一些东西。这可能对你没有意义。不知道我有什么问题。一开始的一些东西是用于其他功能。我以前尝试过的一些东西都在评论中。

usefulness: { usefulResult:-3 , notUseful: 6, useful: 3}

从save中返回的对象:

    Reviews.findOne({companyName: comp , user : userId}).populate({
        path : "user",
        model : "Anon"
    })
    .then(function(returnedReview){
        console.log("returnedReview", returnedReview)
        // var returnedModel = new Reviews
        // returnedReview.usefulness = {}
        var mapped = returnedReview.userWhoVoted.toObject().map(function(e) {return e.userWhoVotedId.toString()})
        if(mapped.indexOf(app.locals.user._id.toString()) == -1){
            console.log("typeof app.locals.user.id---", typeof app.locals.user._id.toString())
            mapped.push(app.locals.user._id.toString())
        }
        var index = mapped.indexOf(app.locals.user._id.toString())
        console.log("--------INDEX----------", index)
        console.log("mapped User that voted?? --" , mapped)
        var checkForuserWhoVotedId= returnedReview.userWhoVoted.toObject().some(function(el){
            return el.userWhoVotedId.toString() == app.locals.user._id.toString()
        })
        var checkForWordUseful = returnedReview.userWhoVoted.toObject().some(function(el){
            return el.result == "useful" && el.userWhoVotedId.toString() == app.locals.user._id;
        })
        var checkForWordNotUsefull = returnedReview.userWhoVoted.toObject().some(function(el){
            return el.result == "notUseful" && el.userWhoVotedId.toString() == app.locals.user._id;
        })

        var userWhoVoted = returnedReview.userWhoVoted;
        var usefulnessObj = returnedReview.usefulness;

        if(userWhoVoted.toObject().length == 0){
            userWhoVoted.push({userWhoVotedId : app.locals.user._id.toString(), result : result})
        }else if(userWhoVoted.toObject().length > 0 && !checkForuserWhoVotedId) {

            userWhoVoted.push({userWhoVotedId : app.locals.user._id, result : result})
        }else if(userWhoVoted.toObject().length > 0 && checkForuserWhoVotedId && result == "notUseful" && checkForWordUseful){
            if(userWhoVoted[index].result){
               var indexed = userWhoVoted[index].result
            }


            userWhoVoted[index].result = "notUseful"
        }else if(userWhoVoted.toObject().length > 0 && checkForuserWhoVotedId && result == "useful" && checkForWordNotUsefull){
            if(userWhoVoted[index].result){
               var indexed = userWhoVoted[index].result
            }
            userWhoVoted[index].result = "useful"
        }

        if(!checkForuserWhoVotedId){
                if(result == "useful") usefulnessObj.useful++
               if(result == "notUseful") usefulnessObj.notUseful++
               userWhoVoted[index].nowUseful = usefulnessObj.useful;
              userWhoVoted[index].nowNotUseful = usefulnessObj.notUseful;
               console.log("usefulnessObj.useful : ",usefulnessObj.useful, "userWhoVoted[index].nowUseful + 1 : ", userWhoVoted[index].nowUseful + 1)
        }else if(checkForuserWhoVotedId){
            // console.log("usefulnessObj.useful : ",usefulnessObj.useful, "userWhoVoted[index].nowUseful + 1 : ", userWhoVoted[index].nowUseful + 1)
            if(result == "useful" && usefulnessObj.useful + 1 <= userWhoVoted[index].nowUseful ){
                usefulnessObj.useful++
                // usefulnessObj.notUseful--
            } 
            if(result == "notUseful" && usefulnessObj.notUseful + 1  <= userWhoVoted[index].nowNotUseful ){
                usefulnessObj.notUseful++
                // usefulnessObj.useful--
            }
            // if(result == "useful" && usefulnessObj.useful > userWhoVoted[index].nowUseful - 1){

            // }
        }

        // var upperBoundUseful = 
        // console.log("userWhoVoted[index].result ", indexed, " result: ", result)
        // if(indexed !== result){
        //     if(userWhoVoted[index].result =="useful"){
        //         returnedReview.usefulness.useful++
        //         returnedReview.usefulness.notUseful--
        //     }else if(userWhoVoted[index].result =="notUseful"){
        //         returnedReview.usefulness.notUseful++
        //         returnedReview.usefulness.useful--
        //     }

        // }   
        // if(indexed !== userWhoVoted[index].result)   




    //    if(result == "useful") returnedReview.usefulness.useful++
    //    if(result == "notUseful") returnedReview.usefulness.notUseful++
       returnedReview.usefulness.usefulResult = returnedReview.usefulness.useful - Math.abs(returnedReview.usefulness.notUseful);
    //    userWhoVoted = []
        returnedReview.save(function(err, doc){
            if(err) throw err;
            if(doc) console.log("doc", doc)
            res.send(doc.usefulness)
            // res.end()
        })

    })
    .catch(function(err){
        console.log(err)
    })
    // res.end()
}

失控了。

1 个答案:

答案 0 :(得分:1)

好的,所以你的问题是你只希望用户有1票,而你正在寻找一个好的结构吗?以下是我将在简化的评论结构上做的事情:

https://jsfiddle.net/WilliamIPark/kuy0d1ec/5/

var review = {
  author: '57c760cf1e08000300525774',
  content:'Some content', 
  usefulness: {
    useful: [
      '57c760cf1e08000300525774',
      '57cfc275e3ec2c3826ea55a0'
    ],
    useless: [
      '57cfc275e308000300a55a7',
      '57c4f982a82a799432999b63',
      '57c4f982f82a799400a55a7'
    ],
    total: function() {
            return this.useful.length - this.useless.length;
    }
  }
}

review.usefulness.total()将吐出计算,只要您在&#34;有用&#34;中只保留1个用户ID实例,它就会准确无误。并且&#34;无用&#34;阵列。

显然,你会有几个功能:

  • 将用户ID添加到review.usefullness对象,review.usefullness.usefulreview.usefullness.useless

  • 检查review.usefullness.usefulreview.usefullness.useless中已存在的用户ID,并添加,不执行任何操作,或从review.usefullness.useful或{{1}中删除取决于支票。