如何在Postgres投票表中增加INT值

时间:2019-04-02 16:35:40

标签: node.js postgresql

我创建了一个用于投票的API,但无法正常工作。每当调用upvote api时,如何实现表决以将no_votes增加1。这是我尝试过的

exports.upvote_question = async (req, res) => {
    const text = `INSERT INTO 
    votes(id, questionId, userId, no_votes, createdOn) 
    VALUES($1, $2, $3, $4, $5) 
    returning *`;
    const votes =`UPDATE votes
    SET no_votes = total + 1
      returning *`;
    const values = [
        uuidv4(),
        req.params.questionId,
        req.params.userId,
        0,
        moment(new Date())
    ];
    try {
        const { rows } = await db.query(text, values);
        return res.status(201).json({
            message: 'vote successful',
            votes: rows[0],
            request: {
                type: 'GET',
                url: 'http://localhost:3000/api/v1/upvote/' + rows[0].id 
            }
        });
    } catch (err) {
        return res.status(400).json({
            error: console.error(err)});
    }
};

0 个答案:

没有答案