Skip to content

Routes Grouping with .post HookTrigger methods doesn't take into account group prefix #54

Closed
@jaysonng

Description

@jaysonng

When grouping methods together with

func post<Response, V>(_ path: PathComponent...,
object: V.Type,
trigger: ParseHookTriggerType,
use closure: @escaping (Request) async throws -> Response) -> Route
where Response: AsyncResponseEncodable, V: ParseObject {
self.on(path,
object: object,
trigger: trigger,
use: closure)
}

the prefix of the group, ie "user"

    func boot(routes: RoutesBuilder) throws {
        
        let usersGroup = routes.grouped("user")

        usersGroup.post("login", "after",
                        object: User.self,
                        trigger: .afterLogin,
                        use: afterLogin
        )
    
    }

Screenshot 2024-01-10 at 10 31 15 AM

which produces a ParseError when called

  Error: {"message":{"status":404,"headers":{"content-length":"35","content-type":"application/json; charset=utf-8","connection":"keep-alive","date":"Wed, 10 Jan 2024 02:20:30 GMT"},"buffer":{"type":"Buffer","data":[123,34,114,101,97,115,111,110,34,58,34,78,111,116,32,70,111,117,110,100,34,44,34,101,114,114,111,114,34,58,116,114,117,101,125]},"text":"{\"reason\":\"Not Found\",\"error\":true}","data":{"reason":"Not Found","error":true}},"code":141} {"className":"_User","error":{"code":141,"message":{"buffer":{"data":[123,34,114,101,97,115,111,110,34,58,34,78,111,116,32,70,111,117,110,100,34,44,34,101,114,114,111,114,34,58,116,114,117,101,125],"type":"Buffer"},"data":{"error":true,"reason":"Not Found"},"headers":{"connection":"keep-alive","content-length":"35","content-type":"application/json; charset=utf-8","date":"Wed, 10 Jan 2024 02:20:30 GMT"},"status":404,"text":"{\"reason\":\"Not Found\",\"error\":true}"}},"triggerType":"afterLogin","user":"WnjQEA6FsE"}
/Users/jayson/Projects/heroku/node_modules/parse-server/lib/ParseServer.js:261
          throw err;
          ^

ParseError: [object Object]
    at resolveError (/Users/jayson/Projects/heroku/node_modules/parse-server/lib/triggers.js:657:17)
    at error (/Users/jayson/Projects/heroku/node_modules/parse-server/lib/triggers.js:431:17)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 141
}

the afterLogin method is the same from the routes.swift but modified to work with Collections

func afterLogin(req: Request) async throws -> ParseHookResponse<Bool> {

        // Note that `ParseHookResponse<Bool>` means a "successful"
        // response will return a "Bool" type. Bool is the standard response with
        // a "true" response meaning everything is okay or continue.
        if let error: ParseHookResponse<Bool> = checkHeaders(req) {
            return error
        }
        let parseRequest = try req.content
            .decode(ParseHookTriggerObjectRequest<User, User>.self)
        
        req.logger.info("A user has logged in: \(parseRequest)")
        
        return ParseHookResponse(success: true)
    }

I tried pinpointing where it all goes wrong in ParseHookTrigger%2BVapor.swift but couldn't.

to make it work, I had to make the code as such:

    func boot(routes: RoutesBuilder) throws {
        
        //let usersGroup = routes.grouped("user")

        routes.post("user", "login", "after",
                        object: User.self,
                        trigger: .afterLogin,
                        use: afterLogin
        )
                
    }
    

thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions