简体   繁体   中英

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

error was so obvious ...

lack of: import Fluent in header of RouteCollection file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM