I’m doing an Alamofire upload to the server and want to decode some JSON that’s sent back in response.
JavaScript
x
13
13
1
AF.upload(multipartFormData: { multiPart in
2
//do upload stuff to the server here
3
}, to: server)
4
.uploadProgress(queue: .main, closure: { progress in
5
//Current upload progress of file
6
print("Upload Progress: (progress.fractionCompleted)")
7
})
8
.responseJSON(completionHandler: { data in
9
guard let JSON = data.result.value else { return }
10
print("JSON IS (JSON)")
11
//decode the JSON here...
12
})
13
On the line where I’m guarding that data.result.value has a value (the JSON response sent from the server), I’m getting a ‘Type of expression is ambiguous without more context’.
The code to send the JSON object from the server looks like this on the Node.js side:
JavaScript
1
11
11
1
app.post('/createCommunity', upload.single('cameraPhoto'), async function (request, response) {
2
// do operations to get required variables
3
var returnObject = {
4
community_id: id,
5
title: title,
6
members: members,
7
image: imageURL
8
}
9
response.send(returnObject)
10
}
11
Any ideas?
Advertisement
Answer
Since you already have a codable/decodable Community
struct, try this approach:
JavaScript
1
7
1
AF.upload(multipartFormData: { multipartFormData in
2
//do upload stuff to the server here
3
}, to: server)
4
.responseDecodable(of: Community.self) { response in
5
debugPrint(response)
6
}
7