# Vapor - Fix - Cannot infer key path type
try await User
.query(on: req.db)
.filter(\.$username == username) // ERROR! Cannot infer key path type from context
.first()
Warning
- Cannot infer key path type from context; consider explicitly specifying a root type
- No exact matches in call to instance method 'filter'
Fix: The import might be missing
import Fluent
# Vapor - JSON Response
Make a class or struct that conforms to Content
struct MyObject: Content {
let title: String
let text: String
}
Return that object
app.get("object") { req async throws -> MyObject in
return MyObject(title: "Hello", text: "World!")
}
JSON Response:
{
"title": "Hello",
"text": "World!"
}
# Vapor - HTML File Response
app.get { req async throws -> Response in
let path = app.directory.publicDirectory + "index.html"
var data: Data = Data()
for try await chunk: ByteBuffer in try await req.fileio.readFile(at: path) {
if let bytes: Data = chunk.getData(at: 0, length: chunk.readableBytes) {
data.append(bytes)
}
}
var headers = HTTPHeaders()
headers.contentType = .html
return Response(status: .ok, headers: headers, body: Response.Body(data: data))
}
# Vapor - .env file in Docker
To ensure that Vapor’s environment variables work inside a Docker container, copy the .env file into the working directory of the container.
Dockerfile
# Copy the .env file into the container's /app directory
COPY .env /app/.env
# Set the working directory
WORKDIR /app
Note:
Place the COPY .env /app/.env instruction before the WORKDIR /app line in your Dockerfile.
# Simple ScrollView Modifier
extension View {
func scrollable() -> some View {
ScrollView {
self
}
}
}
Usage:
SomeView {
// ...
}
.scrollable()
# Add swift package dependencies
dependencies: [
.package(url: "https://github.com/author/aaa.git", from: "x.x.x"),
],
targets: [
.target(
name: "MyCurrentApp",
dependencies: [
// `name` = Packages.swift > products > .library > name > "LibraryName"
// `package` = package name in url `/repo-name.git`
.product(name: "LibraryName", package: "repo-name"),
],
)
]
# Add or subtract to date
extension Date {
/// Returns a Date with the specified amount of components added to the one it is called with
func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
let components = DateComponents(year: years, month: months, day: days, hour: hours, minute: minutes, second: seconds)
return Calendar.current.date(byAdding: components, to: self)
}
/// Returns a Date with the specified amount of components subtracted from the one it is called with
func subtract(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
return add(years: -years, months: -months, days: -days, hours: -hours, minutes: -minutes, seconds: -seconds)
}
}
Usage:
// Add one year and a half to the current Date
Date().add(years: 1, months: 6)
// Add 30 seconds to the current Date
Date().add(seconds: 30)
// Substract 10 minutes from now
Date().add(minutes: -10)
// or
Date().sustract(minutes: 10)