1

Being new to Swift and Vapor, I'm experimenting with a Vapor project. It has a simple route, which fetches data from a DB via MySQL for Swift, then passes the ResultSet to the Leaf template. Here's what I am attempting:

drop.get("report") {req in
    let data = try mysql.execute("select * from things")
    return try drop.view.make("report", ["data":data])
}

But Swift complains with this error:

error: cannot convert value of type '[[String : Node]]' to expected dictionary value type 'Node'
return try drop.view.make("report", ["data":data])
                                            ^~~~

It feels like I'm missing a type-casting step, but what is it?

Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
dirksen
  • 153
  • 1
  • 6

1 Answers1

1

You should be able to fix this by manually setting the type of the dictionary array to Node:

drop.get("report") {req in
    let data = try mysql.execute("select * from things")
    let dataNode = Node.array(data.map({ return Node.object($0) }))
    return try drop.view.make("report", ["data":dataNode])
}
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • Thanks for the tips, but I get a new error: `error: cannot convert value of type '[[String : Node]]' to specified type 'Node' ` – dirksen Mar 20 '17 at 15:31
  • @user400702 I missed that the dictionaries are in an array. I think I fixed the issue in my answer. – Caleb Kleveter Mar 20 '17 at 16:07
  • 1
    That does it! Thank you very much! Regarding the line Node.array(data.map(...)), does it cause much performance penalty (especially when looping through large ResultSet)? For me, its main purpose seems just to appease the compiler. – dirksen Mar 20 '17 at 19:10
  • @user400702 I would think it would make a performance lose, but I can't easily test on a large set. If you could run the code before the route is even called, or load it later with a socket connection, that would definitely help. – Caleb Kleveter Mar 20 '17 at 19:28