如何在点击事件中将特定JSON对象的ID传递给mongo?

时间:2018-09-14 00:28:46

标签: javascript mongodb

我正在制作一个投票系统,单击向上箭头,我想抓住_id,然后将那个_id传递给我的upvote函数。以下是我与邮递员一起使用的upvote函数。

const db = require('./../helpers/db');
const {ObjectId} = require('mongodb');
module.exports = {

// Get this user id and update code below, also figure out best way to get game id that has been clicked on, maybe assigning that to an id in the html?
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);
        }
    });
},

我的路线在这里:

const auth = require('./../../middleware/auth');
const votingController = require('./../../controllers/voting');

module.exports = function(app, passport, config) {

    app.post('/api/upvote', votingController.upvote);

    app.post('/api/downvote', votingController.downvote);

    // app.get('/api/users/:userId', usersController.getById);

我的一些html在这里:

<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>

所以我想onclick“ super” id,基本上是将_id从类传递给upvote函数。如何正确执行此操作?这对我来说真的很难。

0 个答案:

没有答案