Ecoding of document with StringTranscoder failed

Hi,
I am using couchbase with scala,my code looks like this

  def couch = Action {
  
 var s=

     accBucket.onComplete{
       case Success(value) =>value.get[StringDocument]("d1")
       case Failure(e) => e.printStackTrace
     }
  
 Ok(s.toString).as("text/html")
  }

I am getting error as below

image

can anyone help me to solve this

Thank you in advance

I also tried like this

  def couch2 = Action {
   
    val s=

      accBucket.onComplete{
             case Success(value) =>value.getJsT[Acc]("age").map(json => Json.parse(json.toString))
        case Failure(e) => e.printStackTrace
      }

    Ok(s.toString).as("text/html")
  }

but i am getting output as

image

Hey @an.sowmya,

I’d like to help but this code snippet is a little too isolated. Could you please give a more complete example? I.e. something I can C&P into a project and fully understand.

Though as a starting point you probably want to be using JsonDocument rather than StringDocument, as your content appears to be Json.

Hi @graham.pople ,

          Thank you for reply to my post

In couchbase i am storing document like this

image

I need to retreive the data in play framework(scala),

so i have written the action in the controller like this

package controllers
import com.couchbase.client.java.document.JsonDocument
import com.couchbase.client.java.document.json.JsonObject
import com.couchbase.client.java.env.{CouchbaseEnvironment, DefaultCouchbaseEnvironment}
import com.couchbase.client.java.query.{N1qlQuery, N1qlQueryResult, N1qlQueryRow}
import com.couchbase.client.java.{Bucket, Cluster, CouchbaseCluster}
import com.google.inject.AbstractModule
import com.sandinh.couchbase.CBCluster
import com.typesafe.config.{Config, ConfigFactory}
import javax.inject.{Inject, Singleton}
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, ControllerComponents}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}


case class Acc(name: String, age: Option[Boolean])
object Acc { implicit val fmt = Json.format[Acc] }

  class CouchbaseController @Inject() (cc: ControllerComponents) extends AbstractController(cc) {
  val config = ConfigFactory.load()
  val cluster: CBCluster = new CBCluster (config)
  val accBucket = cluster.openBucket("acc")

 def couch = Action {
  
 val s=

     accBucket.onComplete{
       case Success(value) =>value.get[JsonDocument]("d1")
       case Failure(e) => e.printStackTrace
     }
 
 Ok(s.toString).as("text/html")
  }

When i execute http://localhost:9000 /couch , i am getting output as ()

But now it is working for

  def couch3=Action{
   val    env:CouchbaseEnvironment=DefaultCouchbaseEnvironment.builder().connectTimeout(10000).build()
      val c: Cluster=CouchbaseCluster.create(env,"http://127.0.0.1:8091")
      val b:Bucket=c.openBucket("acc","Admin@123")

  Ok(b.get("d1").content().toString)}

when i tried http://localhost:9000/couch3 ,i am getting o/p like

{"name":"Tom","age":"5"}

Hi @an.sowmya

Thanks for sending the more complete code through, it gives me a much better idea what’s going on. Now I’m not very familiar with Play so I could be wrong on this, but it looks like CBCluster are some Play-provided wrappers around the Couchbase API that seem to return Scala Futures. Your second example, the working one, uses the Couchbase API directly.

Your second example is also using the blocking Couchbase API (as a side note, we also have an asynchronous API based around the reactive library RxJava). My best guess is that you’re not correctly handling Play’s async Future’s in the first example, which is why that’s failing. Play probably has a way of streaming the results of a Future to the output, or you could use Await to block on the result.

This discussion has helped me clearing my doubts. Thanks.!

Thank you @graham.pople

I will try

asynchronous API based around the reactive library RxJava