vapor / fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
MIT License
146 stars 53 forks source link

Cannot convert value of type '(_) - when use query inside compactMap #98

Closed ahmedraad closed 5 years ago

ahmedraad commented 5 years ago

I've this method here, it shows an error

func all(_ req: Request) throws -> Future<[Journey.Public]> {
        return Journey.query(on: req)
            .join(\User.id, to: \Journey.userID)
            .alsoDecode(User.self)
            .all()
            .flatMap(to: [Journey.Public].self, { data in

                return try data.compactMap({ args in

                    let (journey, user) = args

                    return try user.cars.query(on: req).first().map(to: Journey.Public.self, { car in
                        return Journey.Public(journey, u: User.DisplayUser(user, car: car, rating: "0"))
                    })

                }).flatten(on: req)
         })
 }
screen shot 2018-10-13 at 8 39 52 pm

Cannot convert value of type '(_) throws -> EventLoopFuture<Void>' to expected argument type '([(Journey, User)]) throws -> EventLoopFuture<_>'

So the guys on discord gave me a bunch of solutions, but non of them work. btw Journey.Public, DisplayUser structs are confirmed to Content

Vapor Toolbox: 3.1.10 Vapor 3.1.0 FluentPostgreSQL 1.0.0

tanner0101 commented 5 years ago

Try adding explicit return types to the closures to aid the compiler in generating a better error message:

{ () -> Foo in
    return someFoo
}
ahmedraad commented 5 years ago

it works now by adding return type -> EventLoopFuture<Journey.Public> to data.compactMap

@tanner0101 thanks!