Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaf/LeafEncoder.swift:162: Fatal error after passing DB-Data to a Leaf-View #218

Closed
j-goette opened this issue Sep 28, 2022 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@j-goette
Copy link

j-goette commented Sep 28, 2022

Hi together,

When I try to pass any object to a view via render function, I get the error: "Leaf/LeafEncoder.swift:162: Fatal error."

In this example, I pull a list of cocktails from the database, put them into context and try to pass them to the view:

app.get("dashboard")` { req async throws -> View in
        let result = try await Cocktail.query(on: req.db).all()
        let context = CocktailContext(cocktails: result)
        
        return try await req.view.render("dashboard", context)
    }

Here is my Context Struct:

struct CocktailContext: Codable, Content {
    let cocktails: [Cocktail]
}

Here is my Cocktail Model:

import Fluent
import Vapor

final class Cocktail: Model, Content, Codable {
    static let schema = "cocktail"
    
    @ID(key: .id)
    var id: UUID?

    @Field(key: "name")
    var name: String
    
    @Field(key: "description")
    var description: String
    
    @Field(key: "amount_ml")
    var amount_ml: Int
    
    @Field(key: "img_url")
    var img_url: String
    
    @OptionalField(key: "video_url")
    var video_url: String?
    
    @Parent(key: "difficulty_id")
    var difficulty_id: Difficulty
    
    @Parent(key: "glass_id")
    var glass_id: Glass
}

This happens not only with the database output but also when I try to create a cocktail object directly, for example. Or with other Object:

return try await req.view.render("dashboard", Difficulty(id: UUID(), difficulty: "med"))

Even if it is deliberate behavior, there is no information about what the error is! I mean, I may make a mistake, but since the application throws a fatal error without description at this point, I have no idea how to deal with it.

I use Vapor 4.65.0 and Leaf 4.2.0 on MacOS Monterey

Kind regards

Jannik

@j-goette j-goette added the bug Something isn't working label Sep 28, 2022
@0xTim
Copy link
Member

0xTim commented Sep 29, 2022

This looks like it was fixed in 4.2.1 - https://github.com/vapor/leaf/releases/tag/4.2.1 - please try upgrading

@j-goette
Copy link
Author

j-goette commented Oct 3, 2022

Jup. Now it is working! Sorry for this useless Issues Post.

@j-goette j-goette closed this as completed Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants