所以我正在尝试使用从我的 Node JS 服务器获取的数据创建一个内容提要。
在这里,我从我的 API 中获取数据
class Webservice {
func getAllPosts(completion: @escaping ([Post]) -> ()) {
guard let url = URL(string: "http://localhost:8000/albums")
else {
fatalError("URL is not correct!")
}
URLSession.shared.dataTask(with: url) { data, _, _ in
let posts = try!
JSONDecoder().decode([Post].self, from: data!); DispatchQueue.main.async {
completion(posts)
}
}.resume()
}
}
Run Code Online (Sandbox Code Playgroud)
将变量设置为从 API 获取的数据
final class PostListViewModel: ObservableObject {
init() {
fetchPosts()
}
@Published var posts = [Post]()
private func fetchPosts() {
Webservice().getAllPosts {
self.posts = $0
}
}
} …Run Code Online (Sandbox Code Playgroud) 我将图像保存到文件对象,然后使用该对象将其保存到服务器。
(ReactJS)
onChange(event) {
this.setState({
file: event.target.files[0],
loaded: 0,
});
}
onClickHandler = () => {
const { match: { params} } = this.props;
const data = new FormData()
console.log(this.state.file)
data.append('file', this.state.file)
axios.post(`http://localhost:3000/albums/${params.albumId}/upload`, data, {
}).then(res => {
console.log(res.statusText)
})
}
Run Code Online (Sandbox Code Playgroud)
(NodeJS - image-upload.js)
const getImageByAlbumId = (request, response) => {
const { id } = request.params;
db.pool.query('SELECT * FROM file WHERE album_id = $1 ORDER BY album_id ASC', [id], (error, results) => {
if (error) {
throw error
} …Run Code Online (Sandbox Code Playgroud)