Skip to content
Advertisement

Alamofire upload JSON response not compiling

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?

Advertisement

Answer

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)     
    }
Advertisement