0

I'm doing an Alamofire upload to the server and want to decode some JSON that's sent back in response.

AF.upload(multipartFormData: { multiPart in
    //do upload stuff to the server here
        }, to: server)
        .uploadProgress(queue: .main, closure: { progress in
            //Current upload progress of file
            print("Upload Progress: \(progress.fractionCompleted)")
        })
        .responseJSON(completionHandler: { data in
            guard let JSON = data.result.value else { return }
            print("JSON IS \(JSON)")
            //decode the JSON here...
        })

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:

app.post('/createCommunity', upload.single('cameraPhoto'), async function (request, response) {
    // do operations to get required variables
    var returnObject = {
      community_id: id,
      title: title,
      members: members,
      image: imageURL
    }
    response.send(returnObject)
}

Any ideas?

nickcoding2
  • 142
  • 1
  • 8
  • 34
  • you could try: `guard let JSON = data.result.value as? [String: Any] else { return }` or another specific dictionary structure to help the compiler. – workingdog support Ukraine Jun 05 '22 at 05:00
  • 1
    There is no `result.value` property, unless you've added it yourself. – Jon Shier Jun 05 '22 at 05:55
  • Unrelated, but: `responseJSON` is deprecated in recent Alamofire versions. What value are you trying to get? – Larme Jun 05 '22 at 09:11
  • @Larme Serverside, I'm just doing a classic Node.js response.send(returnedJSONObject). The 'data' in the completion handler is of some weird AFData type which isn't convertible to data so I don't even know how to extract that JSON object that I want... – nickcoding2 Jun 05 '22 at 11:37
  • @JonShier See my answer above. I'm sending a JSON object as a response and trying to get at it on the swift side. – nickcoding2 Jun 05 '22 at 12:02
  • @workingdogsupportUkraine See my answer above. I'm sending a JSON object as a response and trying to get at it on the swift side. – nickcoding2 Jun 05 '22 at 12:02
  • can you show an example of the JSON object your server is sending as a response to your `upload` request. – workingdog support Ukraine Jun 05 '22 at 12:47
  • can you explain what type each element is, e.g. `members` seems to be an array of some type. My thinking here, is to create a `decodable` struct for the response. – workingdog support Ukraine Jun 05 '22 at 13:03
  • @workingdogsupportUkraine I have a codable struct called 'Community' on the swift side, I just need to know where/how to decode it in the .responseJSON completion handler, you know? The types of the variables in the struct are community_id (Int), title (String), members ([String]), and image (String). – nickcoding2 Jun 05 '22 at 13:10
  • 1
    could you try this: `AF.upload(multipartFormData: { multipartFormData in //do upload stuff to the server here }, to: server) .responseDecodable(of: Community.self) { response in debugPrint(response) }` – workingdog support Ukraine Jun 05 '22 at 13:13
  • @workingdogsupportUkraine This was the answer. Put it in an actual answer instead of a comment and I'll mark it as correct :) thx – nickcoding2 Jun 05 '22 at 13:43

1 Answers1

1

Since you already have a codable/decodable Community struct, try this approach:

    AF.upload(multipartFormData: { multipartFormData in  
    //do upload stuff to the server here 
    }, to: server)  
   .responseDecodable(of: Community.self) { response in 
        debugPrint(response)     
    }