【问题标题】:Deployd comparison and swift with alamofire部署比较和 swift 与 alamofire
【发布时间】:2015-10-05 14:06:54
【问题描述】:

我正在尝试使用 alamofire 从我的已部署 API 中查询数据。如何在请求中进行比较。我有类似的东西:

let parameters = ["number": ["gt": 3]]
 Manager.sharedInstance.request(.GET, "http://localhost:2403/collections", parameters: parameters).responseJSON { (request, response, result) -> Void in
            print(result.isSuccess)
            print(result.data)
        }

但是结果是空的。在我的仪表板中,我有一个数字列,其值为:1、2、3 和 4。因此响应应该返回我的数字为 4 的行。

有什么想法吗? 谢谢

【问题讨论】:

    标签: swift mongodb rest alamofire deployd


    【解决方案1】:

    您需要提取value 而不是data。在 Alamofire 2.0 中,data 仅在 .Failure 案例中可用。这一切都在 Alamofire 3.0 中进行了重新设计,它改用了 Response 对象。

    let parameters = ["number": ["gt": 3]]
    let URLString = "http://localhost:2403/collections"
    
    Manager.sharedInstance.request(.GET, URLString, parameters: parameters)
        .responseJSON { (request, response, result) -> Void in
            print(result.isSuccess)
            print(result.data)
            print("JSON: \(result.value)")
            print("Error: \(result.error)")
        }
    

    【讨论】:

    • 它没有用。控制台什么也不打印,JSON: Optional(())
    • 那你需要打印出error。那应该给你更多信息。此外,我刚刚注意到您使用的是.GET,而使用.PUT.POST 会更有意义。
    • 使用 .PUT 还是 .POST?文档建议使用 GET 来完成。见他的docs.deployd.com/docs/collections/reference/…。我尝试使用 POST 但我收到错误消息:JSON: Optional({ message = "must provide id to update an object"; status = 400; }).使用 GET,错误为零
    • 您的参数对于GET 来说有点奇怪,这就是我想知道您是否使用了错误的 HTTP 方法的原因。如果您的错误为零,那么您需要尝试找出数据中返回的内容。您可以将其转换为字符串以打印出来或使用 responseString 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2023-03-11
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2020-12-30
    相关资源
    最近更新 更多