• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Need help with SQL / javascript


New Coder
Good day
Writing this code there one of the database field is vote - within the javascript i have this parameter called increment votes. When any number is passed through the parameter the votes for that id within the table must be increased by the the parameter amount (or decrease). Got everything working but not sure how to add the inc_vote paramter into my sql. If anyone can help me with that part of the sql statement will appreciate it. Currently studying java script:

  const incVoteById = (article_id, inc_votes) => {

        if (inc_votes === undefined) throw ({status: 400, msg: 'invalid input syntax for type'})

        return db.query('UPDATE article SET votes = ${inc_votes} WHERE article_id= $1 RETURNING *;',[article_id])

            .then(([article]) => {
                if (article === undefined) {
                    throw ({ status: 404, msg: 'article not found' })
                else return article;

how can i add the part in bold so it gets accepted by the sql statement
and for the testing this is how i am testing to see the outcome:

it.only('PATCH 200 - responds with an object containing the updated article (article 1))', () => {
      return request(app)
          .send({ inc_votes: 100 })
          .then(({body}) => {
                      article_id: expect.any(Number),
                      title: expect.any(String),
                      body: expect.any(String),
                      votes: expect.any(Number),
                      topic: expect.any(String),
                      author: expect.any(String),
                      created_at: expect.any(String)


Let's hope i got you right here.

return db.query('UPDATE article SET votes = votes + $1 WHERE article_id = $2 RETURNING *;', [inc_votes, article_id])

By this adjustment, the function incVoteById should update the votes for the article with the given article_id by the specified inc_votes value. But note that this does not validate the value itself, so you need to do it yourself.
Top Bottom