2016-10-20 36 views
0

我試圖從MongoDB中加載一些值,然後通過一些控制器操作將它們作爲JSON提供。我得到的錯誤:重載的方法值[訂閱]無法應用

Overloaded method value [subscribe] cannot be applied to 
(
    org.mongodb.scala.bson.collection.immutable.Document => Unit, 
    Throwable => Unit, 
() => Unit 
) 

雖然對我來說一切看起來應該是工作。

這裏是我的控制器:

package controllers 

import play.api.mvc._ 
import org.mongodb.scala.bson.collection.immutable.Document 
import data.NoteStore 

class NotesController extends Controller { 

    def index = Action { 
    NoteStore.find.subscribe(
     (note: Document) => println(note.toJson), 
     (error: Throwable) => println(s"Query failed: ${error.getMessage}"), 
    () => println("Done") 
    ) 
    } 
} 

而且NoteStore

package data 

import org.mongodb.scala.model.Filters._ 

object NoteStore extends MongoStore { 
    def find = { 
    db("note-io").find 
    } 

    def findOne(id: Long) = { 
    db("note-io").find(equal("id", id)).first 
    } 
} 

對我來說,它看起來像我已經傳給subscribe的參數是不正確的?但在網上查找我無法弄清楚爲什麼,這對我來說很合適。

回答

0

望着文檔MongoCollection.find返回Observable具有訂閱方法有以下特徵,

def subscribe(observer: Observer[_ >: TResult]): Unit 

// and 

def subscribe(observer: com.mongodb.async.client.Observer[_ >: TResult]): Unit 

這清楚地表明,你提供錯誤的參數。因爲它需要一個Observer

collection.find().subscribe(
    new Observer[Document](){ 
     override def onSubscribe(subscription: Subscription): Unit = { 
     // probably some logging or something else that you want on subscription 
     } 

     override def onNext(document: Document): Unit = println(document.toJson()) 

     override def onError(e: Throwable): Unit = println(s"Error: $e") 

     override def onComplete(): Unit = println("Completed") 
    } 
)