gpt4 book ai didi

swift - 混淆 map 与平面 map : Cannot invoke 'flatMap' with an argument list of type '(() -> EventLoopFuture)'

转载 作者:行者123 更新时间:2023-11-28 05:41:47 27 4
gpt4 key购买 nike

我是 Swift 初学者,在 Vapor 框架中工作,开发了一个注册过程,该过程需要几个连续的步骤才能工作。我遇到了以下代码的问题,Xcode 指示标记行上存在错误:

func signup(_ req: Request) throws -> Future<RecordA> {
return try req.content.decode(RecordACreation.self).flatMap(to: RecordACreation.self) { recordACreation in

// Prevent a recordA email from being used twice.
RecordA(on: req).filter(\.email == recordACreation).first().map(to: RecordA.self) { optionalExistingRecordAByEmail in
if let _ = optionalExistingRecordAByEmail {
throw Abort(.unprocessableEntity, reason: "RecordA email already exists")
}

}.flatMap { // Xcode indicates there's an error here.
// Prevent an recordB email from being used twice.
return RecordB.query(on: req).filter(\.email == recordACreation.recordBEmail).first().map(to: RecordA.self) { optionalExistingRecordBByEmail in
if let _ = optionalExistingRecordBByEmail {
throw Abort(.unprocessableEntity, reason: "RecordB email already exists.")
}
}

}.flatMap {
// If the recordB's password could not be successfully hashed.
guard let recordBPassword = try? BCrypt.hash(recordACreation.recordBPassword) else {
throw Abort(.unprocessableEntity, reason: "Password was unhashed")
}

// Ensure a verification token is generated successfully.
guard let optionalVerificationToken = try? VerificationTokenService.generate(), let verificationToken = optionalVerificationToken else {
throw Abort(.unprocessableEntity, reason: "Verification token could not be generated.")
}

let recordA = RecordA(name: recordACreation.recordAName, email: recordACreation.recordAEmail)

return req.transaction(on: .sqlite) { conn in
// TODO: Send email on signup success.

return recordA.save(on: conn).map(to: RecordA.self) { savedRecordA in
guard let recordAId = recordA.id else {
throw Abort(.unprocessableEntity, reason: "Verification token could not be generated.")
}

_ = RecordB(name: recordACreation.recordBName, email: recordACreation.recordBEmail, password: recordBPassword, recordAId: recordAId, verificationToken: verificationToken).save(on: conn)
return savedRecordA
}
}
}
}
}

错误如下:

Cannot invoke 'flatMap' with an argument list of type '(() -> EventLoopFuture)'

I don't entirely understand the mechanisms between an appropriate selection of map and flatMap, so that may be the cause of the issue here.想法?

最佳答案

问题是 RecordA 查询之后的 map 调用:

RecordA(on: req).filter(\.email == recordACreation).first().map(to: RecordA.self) { optionalExistingRecordAByEmail in
if let _ = optionalExistingRecordAByEmail {
throw Abort(.unprocessableEntity, reason: "RecordA email already exists")
}
}

您将 RecordA.self 传递给 to 参数,它将闭包的返回类型设置为 RecordA。相反,您不会返回任何东西。您应该删除 to 参数,它应该可以工作:

RecordA(on: req).filter(\.email == recordACreation).first().map { optionalExistingRecordAByEmail in
if let _ = optionalExistingRecordAByEmail {
throw Abort(.unprocessableEntity, reason: "RecordA email already exists")
}
}

关于swift - 混淆 map 与平面 map : Cannot invoke 'flatMap' with an argument list of type '(() -> EventLoopFuture)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56440328/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com