Swift,错误(参数标签'(节点:)'不匹配任何可用的重载),in vapor,模型控制器
Swift,error(Argument labels '(node:)' do not match any available overloads),rest in vapor,model Controller
我正在使用 postgreSQL 数据库和 vapor,流利 relation.I 我正在尝试为“模型”(dataPointStorage) 进行扩展“请求”,使其成为 Json 格式。我得到错误(参数标签'(节点:)'不匹配任何可用的重载)。
代码。
dataPointController.swift
import Vapor
import HTTP
final class dataPointController: ResourceRepresentable{
func create(request: Request) throws -> ResponseRepresentable{
var dataPoints = try request.dataPoints()
try dataPoints.save()
return dataPoints as! ResponseRepresentable
}
func show(request: Request, dataPoints: dataPointStorage) throws -> ResponseRepresentable {
return dataPoints as! ResponseRepresentable
}
func update(request: Request, dataPoints: dataPointStorage) throws -> ResponseRepresentable {
let new = try request.dataPoints()
var dataPoints = dataPoints
dataPoints.name = new.name
dataPoints.displayName = new.displayName
dataPoints.content = new.content
try dataPoints.save()
return dataPoints as! ResponseRepresentable
}
func delete(request: Request, dataPoints: dataPointStorage) throws -> ResponseRepresentable {
try dataPoints.delete()
return JSON([:])
}
func index(request: Request) throws -> ResponseRepresentable{
return try JSON(node:dataPointStorage.all())
}
func makeResource() -> Resource<dataPointStorage> {
return Resource(
index: index,
store: create,
show: show,
destroy: delete
)
}
}
extension Request{
func dataPoints() throws -> dataPointStorage {
guard let json = json else { throw Abort.badRequest }
**return try dataPointStorage(node: json)** 'error(Argument labels '(node:)' do not match any available overloads)'
}
}
model.swift
import Foundation
import Vapor
import FluentProvider
import PostgreSQLProvider
final class dataPointStorage: Model, JSONRepresentable, NodeRepresentable {
// var _data: dataPointProtocol?
// var _data:[dataPointProtocol] = []
let storage = Storage()
var id: Node?
var name:String
var displayName:String
public var content: String!
init(node:Node ,content: String, displayName:String, name:String) {
self.id = nil
self.content = content
self.displayName = displayName
self.name = name
}
struct Keys {
static let id = "id"
static let content = "content"
}
func forDataBase() {
let array:[berichtDataPoint] = [intDataPoint(), boolDataPoint(), doubleDataPoint()]
let _ = array[0] as! intDataPoint
let _ = array[1] as! doubleDataPoint
for point in array {
switch point {
case is intDataPoint:
print("int")
case is doubleDataPoint:
print("double")
case is boolDataPoint:
print("bool")
default:
print("error")
}
}
}
func makeRow() throws -> Row {
var row = Row()
try row.set("id", id)
try row.set("displayName", displayName)
try row.set("name", name)
return row
}
init(row: Row) throws {
content = try row.get("content")
displayName = try row.get("displayName")
name = try row.get("name")
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"content": content,
"displayName": displayName,
"name": name
])
}
}
extension dataPointStorage: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { dataPointStorage in
dataPointStorage.id()
dataPointStorage.string("displayName")
dataPointStorage.string("name")
dataPointStorage.string("content")
}
}
static func revert(_ database: Database) throws {
try database.delete(dataPointStorage)
}
}
extension dataPointStorage: JSONConvertible {
convenience init(json: JSON) throws {
try self.init(
**content: json.get(dataPointStorage.content)**
error(Instance member 'content' cannot be used on type 'dataPointStorage')
)
}
func makeJSON() throws -> JSON {
var json = JSON()
try json.set("name", name)
try json.set("displaName", displayName)
try json.set("content", Content)
return json
}
我还在模型中使用扩展 dataPointStorage: JSONConvertible ,其中方便的 init 没有获取内容,而我已经将 var 内容定义为 public,
我不知道我哪里做错了。
在您的模型上,您必须创建一个仅包含 Node 参数的初始化程序。我认为您的模型需要更新为以下代码:
import Vapor
import FluentProvider
final class dataPointStorage: Model, JSONRepresentable, NodeRepresentable {
// var _data: dataPointProtocol?
// var _data:[dataPointProtocol] = []
let storage = Storage()
var id: Node?
var name: String
var displayName: String
public var content: String!
init(node: Node) {
self.content = try node.get(dataPointStorage.Keys.content)
self.displayName = try node.get(dataPointStorage.Keys.displayName)
self.name = try node.get(dataPointStorage.Keys.displayName)
}
struct Keys {
static let id = "id"
static let content = "content"
static let displayName = "displayName"
static let name = "name"
}
//...
}
希望对您有所帮助。
我正在使用 postgreSQL 数据库和 vapor,流利 relation.I 我正在尝试为“模型”(dataPointStorage) 进行扩展“请求”,使其成为 Json 格式。我得到错误(参数标签'(节点:)'不匹配任何可用的重载)。 代码。
dataPointController.swift
import Vapor
import HTTP
final class dataPointController: ResourceRepresentable{
func create(request: Request) throws -> ResponseRepresentable{
var dataPoints = try request.dataPoints()
try dataPoints.save()
return dataPoints as! ResponseRepresentable
}
func show(request: Request, dataPoints: dataPointStorage) throws -> ResponseRepresentable {
return dataPoints as! ResponseRepresentable
}
func update(request: Request, dataPoints: dataPointStorage) throws -> ResponseRepresentable {
let new = try request.dataPoints()
var dataPoints = dataPoints
dataPoints.name = new.name
dataPoints.displayName = new.displayName
dataPoints.content = new.content
try dataPoints.save()
return dataPoints as! ResponseRepresentable
}
func delete(request: Request, dataPoints: dataPointStorage) throws -> ResponseRepresentable {
try dataPoints.delete()
return JSON([:])
}
func index(request: Request) throws -> ResponseRepresentable{
return try JSON(node:dataPointStorage.all())
}
func makeResource() -> Resource<dataPointStorage> {
return Resource(
index: index,
store: create,
show: show,
destroy: delete
)
}
}
extension Request{
func dataPoints() throws -> dataPointStorage {
guard let json = json else { throw Abort.badRequest }
**return try dataPointStorage(node: json)** 'error(Argument labels '(node:)' do not match any available overloads)'
}
}
model.swift
import Foundation
import Vapor
import FluentProvider
import PostgreSQLProvider
final class dataPointStorage: Model, JSONRepresentable, NodeRepresentable {
// var _data: dataPointProtocol?
// var _data:[dataPointProtocol] = []
let storage = Storage()
var id: Node?
var name:String
var displayName:String
public var content: String!
init(node:Node ,content: String, displayName:String, name:String) {
self.id = nil
self.content = content
self.displayName = displayName
self.name = name
}
struct Keys {
static let id = "id"
static let content = "content"
}
func forDataBase() {
let array:[berichtDataPoint] = [intDataPoint(), boolDataPoint(), doubleDataPoint()]
let _ = array[0] as! intDataPoint
let _ = array[1] as! doubleDataPoint
for point in array {
switch point {
case is intDataPoint:
print("int")
case is doubleDataPoint:
print("double")
case is boolDataPoint:
print("bool")
default:
print("error")
}
}
}
func makeRow() throws -> Row {
var row = Row()
try row.set("id", id)
try row.set("displayName", displayName)
try row.set("name", name)
return row
}
init(row: Row) throws {
content = try row.get("content")
displayName = try row.get("displayName")
name = try row.get("name")
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"content": content,
"displayName": displayName,
"name": name
])
}
}
extension dataPointStorage: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { dataPointStorage in
dataPointStorage.id()
dataPointStorage.string("displayName")
dataPointStorage.string("name")
dataPointStorage.string("content")
}
}
static func revert(_ database: Database) throws {
try database.delete(dataPointStorage)
}
}
extension dataPointStorage: JSONConvertible {
convenience init(json: JSON) throws {
try self.init(
**content: json.get(dataPointStorage.content)**
error(Instance member 'content' cannot be used on type 'dataPointStorage')
)
}
func makeJSON() throws -> JSON {
var json = JSON()
try json.set("name", name)
try json.set("displaName", displayName)
try json.set("content", Content)
return json
}
我还在模型中使用扩展 dataPointStorage: JSONConvertible ,其中方便的 init 没有获取内容,而我已经将 var 内容定义为 public, 我不知道我哪里做错了。
在您的模型上,您必须创建一个仅包含 Node 参数的初始化程序。我认为您的模型需要更新为以下代码:
import Vapor
import FluentProvider
final class dataPointStorage: Model, JSONRepresentable, NodeRepresentable {
// var _data: dataPointProtocol?
// var _data:[dataPointProtocol] = []
let storage = Storage()
var id: Node?
var name: String
var displayName: String
public var content: String!
init(node: Node) {
self.content = try node.get(dataPointStorage.Keys.content)
self.displayName = try node.get(dataPointStorage.Keys.displayName)
self.name = try node.get(dataPointStorage.Keys.displayName)
}
struct Keys {
static let id = "id"
static let content = "content"
static let displayName = "displayName"
static let name = "name"
}
//...
}
希望对您有所帮助。