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

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

我现在面临的主要问题是,我想让一个用户只能为一个评论做一个投票,但他们以后可能会改变主意。 我知道如何找出哪个用户正在进行投票。 但我想不出逻辑,所以计数不会增加超过1票。

这是坏的:坏:用户可以继续点击不使用的button,不用的button计数器不断上升。

信息: usefulness对象具有针对特定评论的所有用户的计数,所以它可以已经从先前的数据填充。 所以可以说数据本来是usefulness: { usefulResult:-2 , notUseful: 5, useful: 3},并且一个新的用户投票useful它应该是usefulness: { usefulResult:-1 , notUseful: 5, useful: 4} ,如果他然后改变主意并且投票notUseful usefulness: { usefulResult:-3 , notUseful: 6, useful: 3} notUseful该对象应该变成usefulness: { usefulResult:-3 , notUseful: 6, useful: 3}

我正在和mongoose一起工作和expression。 我试了一堆东西。 这是我正在处理的一些东西。 这可能对你没有意义。 不知道我的错在哪里。 一开始有些东西是用于其他function的。 我以前尝试的一些东西是在评论。

  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() } 

从保存返回的对象:

 doc { _id: 57c760de1e08000300525775, updatedAt: Wed Sep 07 2016 03:32:29 GMT-0400 (Eastern Daylight Time), createdAt: Wed Aug 31 2016 18:57:34 GMT-0400 (Eastern Daylight Time), vote: 'up', reviewText: 'this sisisfrfr', company: 57c4f982a82a799432999b63, companyName: 'comp1', userType: 'anon', user: { _id: 57c760cf1e08000300525774, __v: 1, usefulness: [], reviews: [ 57c760de1e08000300525775 ] }, __v: 2, className: '', createdString: 'We, August 31st 16, 6:57:34 pm', momented: '6 days ago', updatedString: 'We, September 7th 16, 3:32:23 am', userWhoVoted: [ { nowNotUseful: 0, nowUseful: 1, userWhoVotedId: 57cfc275e3ec2c3826ea55a0, result: 'notUseful' } ], usefulness: { usefulResult: 1, notUseful: 0, useful: 1 }, statements: [ { name: 'statement2', question: 'This is the question for statement2', result: 6 }, { name: 'statement3', question: 'This is the question for statement3', result: 9 } ] } 

它失控了。

好的,所以你的问题是你只希望用户有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()将吐出计算,并且只要在“有用的”和“无用的”数组中只保留一个用户id实例,它就是准确的。

显然,你会有一些function:

  • 将用户标识添加到review.usefullness对象中,以review.usefullness.usefulreview.usefullness.useless

  • 检查用户的ID是否存在于review.usefullness.usefulreview.usefullness.useless ,然后添加,不做任何事情,或根据检查从review.usefullness.usefulreview.usefullness.useless删除。