快速蒸气路线错误-类型没有成员“保存”

时间:2019-12-21 15:06:54

标签: swift routes vapor

我在route.swift中遇到以下错误- 如果需要,我还可以发布其他文件-

import Routing
import Vapor
import FluentSQLite

 public func routes(_ router: Router) throws {
 router.get { req -> Future <View> in
 let Newyorker = Pizza(id: 5, name: "statinisland", description: "impracticaljokers", price: 55)
let Traditional = Pizza(id: 5, name: "rome", description: "pope", price: 55)
return try req.view().render("welcome",["pizza":[Newyorker,Traditional]])
}

 router.post(Pizza.self, at: "add") { req, pizza ->
 Future<Response> in
return Pizza.save(on:req).map(to:Response.self) { Pizza
    in
    return req.redirect(to: "/")
  }
 }
 /*Error - Type 'Pizza' has no member 'save'; did you mean 'name'?
Replace 'save' with 'name' */  
  }

无法找出解决方案。

2 个答案:

答案 0 :(得分:2)

除了按照先前的答案将Pizza.save(on:req)更改为pizza.save(on:req)外,您还需要使Pizza模型符合Content。像这样:

struct Pizza:SQLiteModel, Content

或者更好

final class Pizza:SQLiteModel, Content

答案 1 :(得分:1)

Pizza.save(on: req)更改为pizza.save(on: req),因为您要保存发送到闭包的实例