首页 > 解决方案 > Onclick 如何在 mongodb 中查找和更新投票?

问题描述

我正在创建一个投票系统,但我似乎无法找到一种方法来做我想做的事情(或者我正在寻找错误的事情)。基本上我正在尝试点击甚至触发此代码,而不是使用我的虚拟代码

{"_id": ObjectId("5b7b67247a3b516f8cb0c7d9")}

我希望它从我单击的那个中获取 ObjectId。我应该将其分配给 HTML 中的 ID 吗?

upvote: function(req, res, next){
    let userId = db.get().collection('users').findOne({"_id": ObjectId("5b6a39ff17e9af6c20b9c5a8")});
    let gameId = db.get().collection('matches').findOne({"_id": ObjectId("5b7b67247a3b516f8cb0c7d9")});

    userId.then(function(upvotedata){
        if(upvotedata){
            db.get().collection('matches').updateOne({"_id": ObjectId("5b7b67247a3b516f8cb0c7d9")}, { "$inc": { "currentvote": 1}});
            res.status(200).json();

        } else {
            return res.sendStatus(400);
        }
    });
    gameId.then(function(game){
        if(game){
        } else {
            return res.sendStatus(400);
        }
    });
},





<div class="row game-cards featured-game front">
                <div class="container col-lg-6">
                    <canvas id="game-circles"></canvas>
                </div>
                <div class="col-lg-6 game-info">
                    <h6 class="text-center">{{topmatch.AwayTeam}} @ {{topmatch.HomeTeam}}</h6>
                    <p class="text-center">{{topmatch.MatchTime}}</p>
                    <h6 class="text-center">Total Points Bet</h6>
                    <h5 class="text-center">500</h6>
                    <form>
                        <div class="form-group text-center">
                            <label for="home-team">{{topmatch.HomeTeam}} {{topmatch}}</label>
                            <input type="number" class="form-control text-center" placeholder="Home Team Bet">
                        </div>
                        <div class="form-group text-center">
                            <label for="away-team">{{topmatch.AwayTeam}} {{topmatch.Odds.PointSpreadAway}}</label>
                            <input type="number" class="form-control text-center"  placeholder="Away Team Bet">
                            </div>
                            <button type="submit" class="btn btn-primary btn-block">Submit</button>
                            <button type="button" class="btn btn-secondary btn-block play-with-friends">Play With Friends</button>
                    </form>
                </div>
                <div class="col-lg-12">
                    <div class="row">
                        <div action="/api/upvote" method="post" id="super1"><i id="super" class="fas fa-chevron-circle-up color-gray upvote {{topmatch.ID}}"></i></div>
                        <div class="margin-left"><i onclick="upvote()" id="super" class="fas fa-chevron-circle-down color-gray downvote {{topmatch.ID}}"></i></div>
                        <div class="margin-left"><p class="vote-number" id="green">{{topmatch.currentvote}}</p></div>
                    </div>
                </div>
            </div>

标签: javascriptnode.jsmongodb

解决方案


推荐阅读