Hello. I will post to the forum for the first time.
I’m trying to make my own Vapor APP after finishing the book and video tutorial now.
I encountered some things I could not do in it. It is trying to create a class or struct json is nesting in JSON and save it in postgre.
In addition to suffering from errors such as the format of Future not matching, even if you define an empty array in Class and try to save it, it is an error such as different Element.
How do you manage a model that JSON has nested within JSON?
Please let me know if you do not mind.
I created MyAcronymModel and JSON format is
MyAcronym.swift
import Vapor
import Foundation
import FluentPostgreSQL
final class MyAcronym:Codable,Content,PostgreSQLModel,Parameter {
var user:User.Public
var acronyms:[Acronym]
var id:Int?
}
extension MyAcronym:Migration {
}
MyAcronymController.swift
import Foundation
import Vapor
import FluentPostgreSQL
import Authentication
struct MyAcronymControler:RouteCollection {
func boot(router: Router) throws {
let myAcronymRoute = router.grouped("api/myacronym")
let tokenAuthMiddleware = User.tokenAuthMiddleware()
let guardAuthMiddleware = User.guardAuthMiddleware()
let tokenAuthGroup = myAcronymRoute.grouped(tokenAuthMiddleware, guardAuthMiddleware)
/*going to make createMyAcronymRoute
*/
}
func createMyAcronyms(_ req:Request,UserData:User.Public) -> Future<MyAcronym>{
return try req.parameters.next(User.self).map(to:[Acronym].self){ user in
try user.acronyms.query(on: req).all()
let data = myAcronymData(user: UserData, acronyms: queryData)
}
}
}
struct myAcronymData {
var user:User.Public
var acronyms:[Acronym]
}
Multiple errors will be issued at func createMyAcronyms. The rest is the same as the tutorial. How did anyone solve if someone had a similar challenge?