错误无效的数据类型,必须是数组或\\ ArrayAccess实例

问题描述:

当我发送图像和文本数据时,遇到下一个问题时,发生这种情况时,我将数据从角度发送到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

该异常是在线〜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 –