Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vapor Swift Fluent : Type of expression is ambiguous without more context

This is my route :

router.get("answers","delete", Int.parameter) { req -> Future<Response> in

        let answerID = try req.parameters.next(Int.self)
        guard let _ = getUsername(req) else {
            throw Abort(.unauthorized)
        }
        return Message.query(on: req).filter(\.id == answerID).first().map(to: Response.self) { answer in
            guard let answer = answer else {
                throw Abort(.notFound)
            }
            return answer.delete(on: req).map(to: Response.self) { _ in
                return req.redirect(to: "/answers")
            }
        }
    }

But for xCode this is Ambiguous without more context .filter(\.id == answerID)

And this is my struct Message :

struct Message: Content, SQLiteUUIDModel, Migration {
var id: UUID?
var username: String
var content: String
var category: String
var question: String
var date: Date

}

I don't understand because I use filters on ids in many other routes and it's the first time I show this error.

I googled this and it seems that the errors shows up when Fluent is not imported but I've already import Fluent at the top.

like image 387
Thomas Schmitt Avatar asked Nov 29 '18 09:11

Thomas Schmitt


1 Answers

I just had time to test your code. If you replace

...   .map(to: Response.self) { answer in ....

with

... .flatMap(to:Response.self) { answer in ...

it works.

like image 72
LoVo Avatar answered Oct 14 '22 23:10

LoVo