Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每个主题只能让用户投票一次,但他们可以改变主意_Javascript_Arrays_Node.js_Mongodb_Express - Fatal编程技术网

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

Javascript 每个主题只能让用户投票一次,但他们可以改变主意,javascript,arrays,node.js,mongodb,express,Javascript,Arrays,Node.js,Mongodb,Express,我需要逻辑方面的帮助。我有“有用”和“不有用”按钮在我的网站上的评论。我这样做是为了,当用户单击它们时,我会得到一个对象,如有用性:{usefulResult:1,notusefulse:0,useful:1},,因此每次用户单击按钮时,相应的属性都会增加。而usefulResult只是有用-不有用 我现在面临的主要问题是,我希望一个用户只能对一个评论投一票,但他们以后可能会改变主意。我知道如何找出哪个用户正在进行投票。但我想不出逻辑,所以计票不会增加超过1票 这很糟糕: 坏:用户可以继续点击N

我需要逻辑方面的帮助。我有“有用”和“不有用”按钮在我的网站上的评论。我这样做是为了,当用户单击它们时,我会得到一个对象,如
有用性:{usefulResult:1,notusefulse:0,useful:1},
,因此每次用户单击按钮时,相应的属性都会增加。而
usefulResult
只是
有用-不有用

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

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

信息:
有用性
对象具有特定审阅的所有用户的计数,因此可以根据以前的数据填充该对象。因此,让我们假设数据最初是有用的:{usefulResult:-2,无用的:5,有用的:3},并且一个新用户投票
有用的
它应该是有用的:{usefulResult:-1,无用的:5,有用的:4},如果他改变主意,投票
无用的
,那么对象应该变成有用的:{usefulResult:-3,notUseful:6,useful:3}

我正在使用mongoose和express。我尝试了很多东西。以下是我正在使用的一些东西。这可能对你来说没有意义。不知道我有什么问题。开始时有些东西是用于其他功能的。我以前尝试过的一些东西在评论中

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

它失控了。

好吧,你的问题是你只想让用户有一票,而你正在为此寻找一个好的结构?下面是我对简化的评论结构所做的:

var审查={
作者:“57c760cf1e08000300525774”,
内容:'一些内容',
有用性:{
有用的:[
“57c760cf1e08000300525774”,
‘57cfc275e3ec2c3826ea55a0’
],
无用的:[
‘57cfc275e308000300a55a7’,
‘57C4F982A78299439B63’,
‘57c4f982f82a799400a55a7’
],
总计:函数(){
返回this.usiver.length-this.usiver.length;
}
}
}
review.utility.total()
将给出计算结果,只要在“有用”和“无用”数组中只保留1个用户ID实例,计算结果就会准确

显然,您将有两个函数:

  • 将用户ID添加到
    review.usefulness
    对象中,添加到
    review.usefulness.usefulness
    review.usefulness.usefulness

  • 检查
    review.usefulness.usiver
    review.usefulness.usiver
    中已存在的用户ID,然后根据检查添加、不执行任何操作或从
    review.usefulness.usiver
    review.usefulness.usiver


很有趣。这样我就不必做增量(++)。只需在获取结果长度的数组中添加或删除ID。我稍后会处理此问题。我了解您答案的要点了吗?感谢您向我展示了另一种可能有用的方法。正确。只要您正确管理两个数组中的ID以避免重复,这将起作用。如果您查看JSFIDLe代码我添加了一个函数来演示如何通过正确的检查进行投票。
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 } ] }