返回 vapor4 中的特定字段
Returning specific field in vapor4
我不想获取 location
字段,但 return 其他字段。我将如何实现它?
func getperinfo(_ req: Request) throws -> EventLoopFuture<[Info]>{
let user = try req.auth.require(User.self)
return Info.query(on: req.db).filter(\.$user.$id == user.id!).all()
}
型号:
import Foundation
import Fluent
import Vapor
import FluentPostgresDriver
import FluentPostGIS
final class Info:Model,Content{
static let schema = "info"
@ID(key: .id)
var id:UUID?
@Field(key: "姓名")
var name: String
@Field(key: "IG帳號")
var account: String
@Field(key: "頭像")
var picture: String
@Field(key: "年紀")
var age: String
@Field(key: "生日")
var birth: String
@Field(key: "居住城市")
var city: String
@Field(key: "興趣")
var hobby : String
@Field(key:"位置")
var location: GeometricPoint2D
@Parent(key: "user_id")
var user: User
init(){}
init(id:UUID?=nil, name:String, account:String, picture:String ,age:String, birth:String,location: GeometricPoint2D , city:String, hobby:String, userId:UUID){
self.id=id
self.name=name
self.account=account
self.picture=picture
self.age=age
self.birth=birth
self.location=location
self.city=city
self.hobby=hobby
self.$user.id=userId
}
}
您只需创建一个字段较少的新结构,然后将结果映射到其中
struct MyCustomResult: Content {
let id: UUID
let name: String
}
func getperinfo(_ req: Request) throws -> EventLoopFuture<[MyCustomResult]>{
let user = try req.auth.require(User.self)
return Info.query(on: req.db).filter(\.$user.$id == user.id!).all().map {
[=10=].map { MyCustomResult(id: [=10=].id!, name: [=10=].name) }
}
}
我不想获取 location
字段,但 return 其他字段。我将如何实现它?
func getperinfo(_ req: Request) throws -> EventLoopFuture<[Info]>{
let user = try req.auth.require(User.self)
return Info.query(on: req.db).filter(\.$user.$id == user.id!).all()
}
型号:
import Foundation
import Fluent
import Vapor
import FluentPostgresDriver
import FluentPostGIS
final class Info:Model,Content{
static let schema = "info"
@ID(key: .id)
var id:UUID?
@Field(key: "姓名")
var name: String
@Field(key: "IG帳號")
var account: String
@Field(key: "頭像")
var picture: String
@Field(key: "年紀")
var age: String
@Field(key: "生日")
var birth: String
@Field(key: "居住城市")
var city: String
@Field(key: "興趣")
var hobby : String
@Field(key:"位置")
var location: GeometricPoint2D
@Parent(key: "user_id")
var user: User
init(){}
init(id:UUID?=nil, name:String, account:String, picture:String ,age:String, birth:String,location: GeometricPoint2D , city:String, hobby:String, userId:UUID){
self.id=id
self.name=name
self.account=account
self.picture=picture
self.age=age
self.birth=birth
self.location=location
self.city=city
self.hobby=hobby
self.$user.id=userId
}
}
您只需创建一个字段较少的新结构,然后将结果映射到其中
struct MyCustomResult: Content {
let id: UUID
let name: String
}
func getperinfo(_ req: Request) throws -> EventLoopFuture<[MyCustomResult]>{
let user = try req.auth.require(User.self)
return Info.query(on: req.db).filter(\.$user.$id == user.id!).all().map {
[=10=].map { MyCustomResult(id: [=10=].id!, name: [=10=].name) }
}
}