I have an array of customers which follow the following model:
struct GuestListPeople: Codable {
var fName: String
var lName: String
var dob: Date
var waiver: Bool
}
I want to loop through the array each time checking a MongoDB collection, using Mongokitten, to see if the waiver is signed then update the bool to true or false as needed. I have created this function to check the waiver status.
func checkGuestListWaiver(req: Request, col: Collection, firstName: String, lastName: String, dob: Date, orgId: String)throws->Future<Bool>{
col.findOne(["firstName":firstName, "lastName":lastName, "dob": dob ,"orgId":orgId, "waiver":true]).flatMap{ custDoc in
if let cd = custDoc {
//found one so waiver must be true
return req.future().map{
return true
}
}else {
//not found return false.
return req.future().map{
return false
}
}
}
}
My issue is the function returns a Future Bool, and I am not sure how I should handle the future in the for loop.
for var c in guestList {
let w = try CustomerController().checkGuestListWaiver(req: req, col: customerCol, firstName: c.fName, lastName: c.lName, dob: c.dob, orgId: b.orgId)
c.waiver = w
}
I get an error can't assign EventLoopFurture to type Bool. I have tried a few options, but always come back to the same issue of getting a Future back from the database.
You could iterate using flatten
func processGuestList(_ guestList: [Guest], on req: Request) throws -> Future<[Guest]> {
var newList: [Guest] = []
return try guestList.map { guest in
return try CustomerController().checkGuestListWaiver(req: req, col: customerCol, firstName: c.fName, lastName: c.lName, dob: c.dob, orgId: b.orgId).map { w in
var newGuest = guest
newGuest.waiver = w
newList.append(newGuest)
}
}.flatten(on: req).transform(to: newList)
}