json - Swift,错误(参数标签 '(node:)' 与任何可用的重载不匹配),rest in Vapor,模型 Controller

标签 json swift postgresql vapor

我正在使用 postgreSQL 数据库和 Vapor,关系流畅。我正在尝试将“模型”(dataPointStorage) 的扩展“请求”设为 Json 格式。我收到错误(参数标签“(node:)”与任何可用的重载不匹配)。 代码。

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)'
    }

}

模型.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"
    }

    //...
}

希望这有帮助。

关于json - Swift,错误(参数标签 '(node:)' 与任何可用的重载不匹配),rest in Vapor,模型 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46586146/

相关文章:

ios - 我如何获取用户的当前位置,执行本地地理查询,然后将结果应用到 PFQueryTableViewController?

ios - 从 UITableView 加载 UISplitViewController

sql - 类似于数据库中的引用复制

Python - 解析 Json 和 XML 哪个更快?

php - JSON 字符串无效

ios - 在 swift 中扩展类型别名

postgresql - 如何获取一张表中某列的行

sql - 在 PostgreSQL 中选择第 1 列值相同但第 2 列值不同的行

php - 从数据库获取数据作为 json 响应

ios - Json解析后取回数据