0

I'm trying to make 2 queries for 2 different tables inside the same endpoint. I can do this for simple get queries, but not for more complex update/replace queries. Also I'm not sure how to properly handle errors in this case.

Below is what I tried:

function upvotePost(req,res,next){
        /*query 1*/
        r.table('posts').filter(function(post){
          return post('id').eq(someId);
        }).update(
            {"upvotes": r.row("upvotes").add(1)}).run(req._rdbConn)
        /*query 2*/
        .then(function(){
            r.table('users').filter(r.row('login').eq(someUser))
            .update({upvotelist: r.row('upvotelist').changeAt(someId,1)})
            .run(req._rdbConn).then(function(result){
                res.send(JSON.stringify(result));
            })
        }).error(handleError(res))
         .finally(next);
    }

Right now this returns a connection closed error.

João Lima
  • 430
  • 1
  • 6
  • 11

1 Answers1

0

I made it work using async.parallel

function upvotePost(req,res,next){
    async.parallel([
      /*query 1*/
      function(){
        r.table('posts').filter(function(post){
          console.log(req.url);
          return post('pid').eq(parseInt(req.params.id));
        }).update({"upvotes": r.row("upvotes").add(1)}).run(req._rdbConn).then(function(result){
            res.send(JSON.stringify(result));
        })
      },
      /*query 2*/
      function(){
        r.table('users').filter(r.row('login').eq(req.decoded.login))
        .update({upvotelist: r.row('upvotelist').changeAt(parseInt(req.params.id),1)})
        .run(req._rdbConn)
      }
      ], null);
}

(missing error handling)

João Lima
  • 430
  • 1
  • 6
  • 11