Future not returning anything

54 Views Asked by At

trying to fetch result from database and returning the future resultset. But the issue is while accessing future result i am not getting any response.

below is the code snippnet:

  def getAll(): Future[Iterable[Employee]] = {
  Future{
    fetchEmployees()
   }(ec)
  }

  def fetchEmployees(): Iterable[Employee]={
  var empList =  ListBuffer[Employee]()
      db.withConnection{ conn =>
        val statement = conn.createStatement()
        val rs = statement.executeQuery("Select * from Employee")
        while (rs.next()){
          println(rs.getString("EmpCode")+" "+rs.getString("FirstName")+" "+rs.getString("LastName"),rs.getString("Department"))
          val emp = Employee(rs.getString("EmpCode"),rs.getString("FirstName"),rs.getString("LastName"),rs.getString("Department"))
          empList.appended(emp)
        }
      }
   empList
 }

this is where trying to access return future object

def findAll: Future[Iterable[EmployeeResource]] = {
println("Inside resource handler")
repository.getAll().map(iterableEmp => {
  iterableEmp.foreach(emp => println(s"Name is $emp.firstName"))
  iterableEmp.map(emp=>createResource(emp))
})(ec)

}

Prints nothing.

1

There are 1 best solutions below

0
cbley On BEST ANSWER

Look at the doc for the appended method -- and note the term, it is not "append" (like in a command), but "appended", like what if...

def appended[B >: A](elem: B): ListBuffer[B]

A copy of this sequence with an element appended.

Your code:

empList.appended(emp)

creates a new ListBuffer, but you discard its result and your initial list buffer is never actually modified. (It is always a good idea to switch on the -Ywarn-value-discard scalac option!)

You need to use the += operator (or the addOne method).