newbie
newbie

Reputation: 476

How can I call the find query after successful update query with ReactiveMongo and akka, getting None.get exception

I have two queries in my code

1st one is update query

var  modifier = BSONDocument("$set" -> BSONDocument("client_id" -> client_id,
          "access_token" -> access_token,
          "refresh_token" ->refresh_token,
          "inserted_date" -> inserted_date))
        var selecter = BSONDocument("$and" -> BSONArray(BSONDocument("account_id" -> account_id), BSONDocument("refresh_token" -> object.refreshToken)))

 tokensCollection.update(selecter, modifier)

2nd one is find query

 var query = BSONDocument("$and" -> BSONArray(BSONDocument("account_id" -> account_id), BSONDocument("refresh_token" -> refresh_token)))

    val resp = tokensCollection.find(query).one[AccessTokens]
    var result = Await.result(resp, 15 seconds)
    result.get

my 2nd one find query is executed before the 1st query update.I got the issue

method have exception:java.util.NoSuchElementException: None.get

how can I call the find query after successful update of 1st query

Upvotes: 0

Views: 161

Answers (2)

I expect that your tokensCollection.update() call will also return a Future[_] of some kind. Only when that Future completes will your result be guaranteed to be available in the database.

You can serialize the two like this:

val resp = tokensCollection.update(s, m).flatMap(_ => tokensCollection.find(query))

or, using a for comprehension:

for {
  _ <- tokensCollection.update(s, m)
  q <- tokensCollection.find(query)
} yield q

Note that Await is not something that you should use in production; rather, return Futures everywhere and call map on the final result. However, if you're just playing around, it can be useful for debugging.

Upvotes: 2

aaditya
aaditya

Reputation: 773

It is because update takes more time than find query.You must serialise the query.You have to wait for the response from the update query and than after that execute the find query.

Upvotes: 0

Related Questions