Search code examples
swiftvaporvapor-fluent

Vapor filters are ambiguous in context


why this works when directly in router.swift

router.delete("users", User.parameter, "books", Book.parameter, "favourite") { req -> Future<HTTPStatus> in
    try req.parameters.next(User.self).flatMap { user in
        try req.parameters.next(Book.self).flatMap { book in
            UserBookWatched.query(on: req).filter(\.userID == user.id!).filter(\.bookID == book.id!).first().flatMap { books in
                if let books = books {
                    return books.delete(on: req).transform(to: HTTPStatus.noContent)
                }

                return req.future().transform(to: HTTPStatus.notFound)
            }
        }
    }
}

and this one does not work in route controller

// userid hardcoded, book from Book.parameter 
func removeBookFromFavourited(_ req: Request) throws -> Future<HTTPStatus> {
    let user = User(id: StaticUser.id)

    try req.parameters.next(Book.self).flatMap(to: HTTPStatus.self) { book in
        UserBookFavourited.query(on: req)
            .filter(\.userID == user.id!)
            .filter(\.bookID == book.id!)
            .first().flatMap { books in
            if let books = books {
                return books.delete(on: req).transform(to: HTTPStatus.noContent)
            }

            return req.future().transform(to: HTTPStatus.notFound)
        }
    }
}

it drops error that .filter(\.userID == user.id!) "Type of expression is ambiguous in this context"

models are properly defined as pivot

thx in advance for help(edytowane) static user will be replaced with JWT token sent in request headers


Solution

  • error was so obvious ...

    lack of: import Fluent in header of RouteCollection file