1

I am trying to an existing collection with a set of fields.

  1. I am saving data to one collection.
  2. Later I want to update the collection with image data, so I tried to update existing collection.

But I am getting the below exception on the Angular Controller side:

Possibly unhandled rejection: {
    "data":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot POST /image/upload</pre>\n</body>\n</html>\n",
    "status":404,
    "config":{
        "method":"POST",
        "transformResponse":[null],
        "jsonpCallbackParam":"callback",
        "url":"/image/upload",
        "headers":{"Accept":"application/json, text/plain, */*"},
        "data":{
            "model":{"title":"hello"},
            "files":[{}]
        }
    },
    "statusText":"Not Found"
}

Angular code:

$http({
  method: 'PUT',
  url: properties.imageUpload,
  headers: { 'Content-Type': undefined },
  transformRequest: function(data) {
    var formData = new FormData();
    formData.append("proId", $scope.pid);
    formData.append('model', angular.toJson(data.model));
    for (var i = 0; i < data.files.length; i++) {
      formData.append('file', data.files[i]);
      $scope.uploadedFiles.push(data.files[i]);
    }
    console.log('Sending File size :' + data.files.length);
    //formData.append('file', data.files[0]);
    return formData;
  },
  data: { model: { title: 'hello' }, files: $scope.files }

}).then(function(res) {
  $scope.selectedFiles = "/public/images/" + res.data.imagePaths[0];
  //      console.log(res.data.imagePaths[0]);
});



Angular controller code: Below I am calling PUT API inside POST API in order to update the existing collection.

$scope.addProducts = function () {
          var product = $scope.product;
          $http.post('/products/create', $scope.product).then(function (res) {
            console.log(res.data._id);
            $scope.upload(res.data._id);//this contains put request.
          });
        }

Please help.

mayank bisht
  • 784
  • 9
  • 19

0 Answers0