2017-06-14 56 views
-1

當我發送圖像和文本數據時,遇到下一個問題時,發生這種情況時,我將數據從角度發送到cakephp 3並帶有其他服務,但不知道如何解決:錯誤無效的數據類型,必須是數組或\ ArrayAccess實例

Possibly unhandled rejection: { 
    "data":{ 
     "message":"Invalid data type, must be an array or \\ArrayAccess instance.", 
     "url":"/documents/add", 
     "code":500, 
    "file":"C:\\xampp\\htdocs\\frontend_dekma\\app\\vendor\\cakephp\\cakephp\\src\\Utility\\Hash.php", 
     "line":51 
    }, 
    "status":500, 
    "config":{ 
     "method":"POST", 
     "transformResponse":[null], 
     "jsonpCallbackParam":"callback", 
    "headers":{ 
     "Access-Control-Allow-Origin":"*", 
     "Access-Control-Allow-Headers": 
     "Accept, Authorization, Cache-Control, Content-Type, X-Requested-With, x-csrf-token", 
     "Accept":"application/json", 
    "Authorization":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImV4cCI6MTQ5NzYzNzczNX0.im1g6BeHhMAF-MA0jhzYsU15UAH6BS1tByIqbj2acAQ"}, 
    "url":"/app/documents/add", 
    "data":{} 
    }, 
    "statusText":"Internal Server Error"} 

現在的角度有我的服務發送這樣的信息:

.service('fileUpload', ['$http', function ($http) { 
      this.uploadFileToUrl = function(file, uploadUrl){ 
       var formData = new FormData(); 
       console.log(file); 
       // here take the values 
       formData.append('photo', file.photo); 
       formData.append('descripcion', file.descripcion); 
       //show values formData 
       for (var key of formData.entries()) { 
        console.log(key[0] + ', ' + key[1]); 
       } 
       //post url uploadUrl = '/app/documents/add' 
       $http.post(uploadUrl, formData, { 
        transformRequest: angular.identity, 
        headers: { 
         'Access-Control-Allow-Origin' : '*', 
         'Access-Control-Allow-Headers': 'Accept, Authorization, Cache-Control, Content-Type, X-Requested-With, x-csrf-token', 
         'Content-Type': undefined 
        } 
       }) 
       .then(function(response){ 
        console.log(response); 
       }); 
      } 
     }]) 

這是我的後端控制器:

public function add() 
    { 
     $document = $this->Documents->newEntity(); 
     $data = ['result' => 'null', 'id' => 'null']; 
     if ($this->request->is('post')) 
     { 
      $document = $this->Documents->patchEntity($document, $this->request->getData()); 
      if ($this->Documents->save($document)) 
      { 
       $data = ['result' => 'success', 'id' => $document->id]; 
      } else { 
       $data = ['result' => 'error']; 
      } 
     } 
     $this->set(compact('data')); 
     $this->set('_serialize', ['data']); 
    } 

好,我做了一些不同,但是幫助我,以檢測哪裏出了問題

我第一次嘗試使用cakephp-upload。但最初以博客的例子不明白它是如何工作的,所以一開始我遇到了這個我也不理解的錯誤。現在我以不同的方式對待,一切正常,直到我試圖將數據存儲在數據庫中,這裏發生錯誤時,實際上在帖子開始處繼續有相同的錯誤,但現在我知道問題在哪裏:

經過幾個小時的多次測試,我評論這部分並且工作正常,但顯然不會在DataBase上不存儲任何內容。用不同的代碼。現在可以工作,但需要將數據存儲在數據庫中,並瞭解錯誤。

public function add() 
{ 
    $document = $this->Documents->newEntity(); 
    $data = ['result' => $this->request->getData(), 'id' => 'null']; 
    $file = $this->request->getData('photo'); 
    $file['name'] = time() . '-' . str_replace(' ', '_', $file['name']); // timestamp files to prevent clobber 
    if (move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/' . $file['name'])) 
    { 
     //When comment this part dont have error 
     //but can't save in the data base my information 
     // 
     /* 
     $document->photo = $file['name']; 
     $document->descripcion = $this->request->getData('descripcion'); 
     $document = $this->Documents->patchEntity($document, $this->request->getData()); 
     if ($this->Documents->save($document)) { 
      $data = ['result' => 'success', 'id' => $document->id]; 
     } 
     */ 

    } 
    $this->set(compact('data')); 
    $this->set('_serialize', ['data']); 
} 
+0

上傳文件的內容類型應該是multipart/form-data。你試過這個嗎? –

+0

是的,但有其他錯誤'錯誤:$ http:baddata 錯誤的JSON數據 數據必須是有效的JSON對象。收到:' – CoolLife

+0

我不熟悉Angular。我建議你嘗試進一步隔離問題,即確定它是Angular方還是CakePHP方(或者甚至兩者)。首先檢查您的瀏覽器網絡控制檯,並檢查請求是否看起來像預期的/必需的(標題和數據)。 – ndm

回答

0

該異常是在線〜80存在的: /home/user/myapp/vendor/josegonzalez/cakephp-upload/src/Model/Behavior/UploadBehavior.php

public function beforeSave(Event $event, Entity $entity, ArrayObject $options) 
{ 
    foreach ($this->config() as $field => $settings) { 
     if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) { 
      HERE ---> if (Hash::get($settings, 'restoreValueOnFailure', true)) { 

教程的博客是爲早期版本,如果蛋糕或不好。 檢查這個文檔:http://cakephp-upload.readthedocs.io/en/latest/examples.html

在你DocumentsTable.php你應該有這樣的事情:

$this->addBehavior('Josegonzalez/Upload.Upload', [ 
    'path' => [], 
]); 

的「路徑」不應該是一個值,而是一個數組的關鍵:「路徑」 = > [],

+0

我剛剛發現了更多的細節:https://github.com/FriendsOfCake/cakephp-upload/issues/428 –

相關問題