1

为什么这在直接在 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)
            }
        }
    }
}

而这个在路由控制器中不起作用

// 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)
        }
    }
}

它会丢弃 .filter(\.userID == user.id!)“在此上下文中表达式类型不明确”的错误

模型被正确定义为枢轴

提前感谢帮助(edytowane)静态用户将替换为请求标头中发送的 JWT 令牌

4

1 回答 1

4

错误是如此明显......

缺少:import Fluent在 RouteCollection 文件的标头中

于 2019-11-29T18:25:34.057 回答