由于“错误:‘URLSession’类型的值没有成员‘数据’”,将 Vapor 应用程序部署到 Heroku 失败
Posted
技术标签:
【中文标题】由于“错误:‘URLSession’类型的值没有成员‘数据’”,将 Vapor 应用程序部署到 Heroku 失败【英文标题】:Deploying a Vapor app to Heroku fails because of " error: value of type 'URLSession' has no member 'data' " 【发布时间】:2022-01-15 09:25:17 【问题描述】:我使用 Vapor 创建了一个应用程序,我想将其部署到 Heroku,但推送失败。
构建配置如下所示
.swift 版本:5.5 使用 heroku-buildpack日志如下所示。
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: https://github.com/vapor-community/heroku-buildpack
remote: -----> Swift app detected
remote: -----> Using Swift 5.5 (from .swift-version file)
remote: -----> Using built-in clang (Swift 5.5)
remote: -----> Installing swiftenv
remote: -----> Installing Swift 5.5
remote: Downloading https://swift.org/builds/swift-5.5-release/ubuntu2004/swift-5.5-RELEASE/swift-5.5-RELEASE-ubuntu20.04.tar.gz
remote: /tmp/swiftenv-5.5- /tmp/codon/tmp/buildpacks/66275d05cfaec82f8023e1ae68b013a22671c52f
.
.
.
remote: /tmp/build_ecf900fb/Sources/App/APIClient/APIClient.swift:61:34: error: value of type 'URLSession' has no member 'data
remote: return try await session.data(for: request, delegate: nil)
remote: ~~~~~~~ ^~~~
remote: /tmp/build_ecf900fb/Sources/App/APIClient/APIClient.swift:61:63: error: 'nil' requires a contextual type
remote: return try await session.data(for: request, delegate: nil)
remote: ^
remote: ! Push rejected, failed to compile Swift app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to apple-dev-news-ios.
remote:
To https://git.heroku.com/apple-dev-news-ios.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/apple-dev-news-ios.git'
APIClient.swift:61:63:
private let session: URLSession
.
.
.
private func actuallySend(_ request: URLRequest) async throws -> (Data, URLResponse)
.
.
.
return try await session.data(for: request, delegate: nil)
为什么会失败,我们如何才能使推送成功?
“数据”在 Foundation 框架中定义如下。
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
extension URLSession
/// Convenience method to load data using an URLRequest, creates and resumes an URLSessionDataTask internally.
///
/// - Parameter request: The URLRequest for which to load data.
/// - Parameter delegate: Task-specific delegate.
/// - Returns: Data and response.
public func data(for request: URLRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse)
.
.
.
【问题讨论】:
我认为您遇到了开源基金会与 Apple 操作系统上使用的基金会之间存在差异的问题。我认为前者有点滞后。我在Open Source version 中没有看到data
(还)。我想你可能需要使用dataTask
。
【参考方案1】:
Linux 上 Foundation 的异步 API 尚不可用(它们与实际的语言并发功能不同)。
对于 Vapor 应用程序,您不应该真正使用 URLSession 来发出请求,它不适合框架的工作方式。改用 Vapor 的 client(它有异步 API)
【讨论】:
以上是关于由于“错误:‘URLSession’类型的值没有成员‘数据’”,将 Vapor 应用程序部署到 Heroku 失败的主要内容,如果未能解决你的问题,请参考以下文章