I'm having trouble sending a date to Vapor 3 from Swift 5, iOS. Here is the error that I get from Vapor:
POST /admin/test Expected date string to be ISO8601-formatted.
Here is how I'm creating the date in iOS
let today = Date()
let f = ISO8601DateFormatter()
f.formatOptions.insert(.withFractionalSeconds)
let finalDate = f.string(from: today)
Here is my Transaction model in iOS
final class Transaction: Codable, ReflectedStringConvertible {
var id: Int?
var date: String
var amount: Int
var planeID: Int
var userID: UUID
var month: Int
var year: Int
init(date: String, amount: Int, planeID: Int, userID: UUID, month: Int, year: Int) {
self.date = date
self.amount = amount
self.planeID = planeID
self.userID = userID
self.month = month
self.year = year
}
}
Here is my vapor API
func createHandler (_ req: Request, transaction: Transaction) throws -> Future<Transaction> {
return transaction.save(on: req)
}
Here's my Transaction model on the vapor side
final class Transaction: Codable {
var id: Int?
var date: Date
var amount: Int
var planeID: Plane.ID
var userID: User.ID
var month: Int
var year: Int
//fixes Swift 5 bug
typealias Database = PostgreSQLDatabase
init(date: Date, amount: Int, planeID: Plane.ID, userID: User.ID, month: Int = 0, year: Int = 0) {
self.date = date
self.amount = amount
self.planeID = planeID
self.userID = userID
self.month = month
self.year = year
}
}
Any ideas what I'm doing wrong? Is it that I'm sending a string for the date instead of a date? Thanks very much.
The answer was to remove the fractionalSeconds
let today = Date()
let f = ISO8601DateFormatter()
let finalDate = f.string(from: today)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.