0

I am new to Reactivemongo database(2.6), as I am trying to upload/insert json object(which is of key/value pairs, I am sending it to store in database after clicking of Submit button from my UI ) from my localsystem to store in Mongodb using Play Framework/Scala(tried with Play 2.2.3 and 2.3.8). I have tried:

import play.api.libs.json.{JsObject, JsValue, OWrites}
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection._

import scala.concurrent.ExecutionContext.Implicits.global

object Application extends Controller with MongoController {
  def jsinfocollection: JSONCollection = db.collection[JSONCollection]("mycollection")

  implicit val jsvalueWrites = new OWrites[JsValue] {
    println("implicit val jsvalueWrites ...")//getting the message on Play console
    override def writes(o: JsValue): JsObject = o match {
      case o : JsObject => o
      case _ => throw new IllegalArgumentException("Only JsObjects can be stored")
    }
  }

  def createJson = Action.async(parse.json) {
    println("createJson calling...")//getting the message on Play console
    request =>
        jsinfocollection.insert(request.body).map{
    println("jsinfocollection.insert...")//getting the message on Play console
        r => Created(s"JsonInfo is Saved with result $r")
      }
  }
}

I created a collection in Mongodb already like: >db.createCollection("mycollection")

{ "ok" : 1 }

but if I give: >db.mycollection.count() - is giving 0, otherwise if I give: db.mycollection.find() - is giving nothing

Please let me know that how can I insert my json data into my required collection("mycollection"). Thanks in advance.

I am getting the following on console:

implicit val jsvalueWrites ...
createJson calling...
jsinfocollection.insert...
[error] play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: Only JsObjects can be stored

Internal server error, for (POST) [/createJson] 
Dhana
  • 711
  • 3
  • 14
  • 40
  • You are using quite old versions, for Play and ReactiveMongo. – cchantep Mar 07 '16 at 12:32
  • @cchantep, may I know any solution for it as I am using Play(2.2.3) and Mongo(2.6). – Dhana Mar 08 '16 at 09:32
  • I doubt you will get many community support with deprecated versions – cchantep Mar 08 '16 at 09:48
  • @cchantep, ok, can I have anything about it on new versions ? – Dhana Mar 08 '16 at 09:50
  • @cchantep, as my Application requires those plugin versions only. – Dhana Mar 08 '16 at 10:18
  • @cchantep, I have tried with Play 2.3.8 also(as I cannot go with 2.4 version as my application requires many some complexity functionality changes), but I am getting the above error that I have described in question and description now. Kindly let me know if anything is required. – Dhana Mar 09 '16 at 12:17
  • 1
    Your error as nothing to do with Mongo.Please read the Play doc about Actions. – cchantep Mar 09 '16 at 12:29
  • @cchantep, I have got the solution now, as I am giving: JSON.stringify() instead of giving normal json in my jQuery/ajax call, and now it is storing fine. Thanks for your help and support ! – Dhana Mar 10 '16 at 07:14

1 Answers1

1

As you are using Action.async(parse.json) you have a JsValue in the request.body

jsinfocollection is only able to store JsObjects (JsObjects are only one type of JsValues, other types are JsArray, JsString, ...)

The following code should do what you are looking for

import play.api.libs.json.{JsObject, JsValue, OWrites}
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection._

import scala.concurrent.ExecutionContext.Implicits.global

object Application extends Controller with MongoController {
  def jsinfocollection: JSONCollection = db.collection[JSONCollection]("mycollection")

  implicit val jsvalueWrites = new OWrites[JsValue] {
    override def writes(o: JsValue): JsObject = o match {
      case o : JsObject => o
      case _ => throw new IllegalArgumentException("Only JsObjects can be stored")
    }
  }

  def createJson = Action.async(parse.json) {
    request =>
      jsinfocollection.insert(request.body).map{
        r => Created(s"JsonInfo is Saved with result $r")
      }
  }
}

Maybe there is a simpler way to create the jsvalueWrites No Json serializer as JsObject found for type play.api.libs.json.JsObject And maybe updating to the latest version helps

Note: Validation that the string is a valid JsValue will be done by the framework in parse.json, validation that is an object is done in jsvalueWrites if you need total control you could implement OWrites[String] and remove the parse.json

Community
  • 1
  • 1
Jonas Anso
  • 2,057
  • 14
  • 13
  • thanks for your reply, I have tried the above thing, but I am getting error like: value asJson is not a member of play.api.libs.json.JsValue, I have updated the same in question and description. I am using Play 2.2.3 and MongoDB 2.6. Please let me know. – Dhana Mar 08 '16 at 03:11
  • Sorry. As you are doing Action.async(parse.json) you already have a JsValue in the request.body. Validation will be done outside your code. I will update my answer – Jonas Anso Mar 08 '16 at 12:13
  • Yeah, sure kindly let me know. Thanks in advance. – Dhana Mar 08 '16 at 12:43
  • Check it know. @Dhana. – Jonas Anso Mar 08 '16 at 13:16
  • I have done the above, but getting: play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: Only JsObjects can be stored at r => Created(s"JsonInfo is Saved with result $r") hopefully, since I can get println message after insert() statement. I am not sure what's the wrong here, please let me know. – Dhana Mar 09 '16 at 05:17
  • Could I know about it please, otherwise can we have chat discussion once please ? – Dhana Mar 09 '16 at 10:45
  • I have updated the question and description for the same, kindly let me know once. – Dhana Mar 09 '16 at 12:08
  • 1
    If you are getting this error is because the body you are sending does not contain a json object. – Jonas Anso Mar 09 '16 at 14:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105815/discussion-between-jonasanso-and-dhana). – Jonas Anso Mar 09 '16 at 14:21
  • the solution you have provided here is working very well now. Thanks for your kind support and help, Hats off !! – Dhana Mar 10 '16 at 07:05
  • Yeah, sure, Thank you !! – Dhana Mar 10 '16 at 09:52