2015-07-16 14 views
7

Sto tornando un array di oggetti dal server:

[{id: 1, name: "name"},{id: 2, name: "name2"}] 

Ora uso angolare di risorse $query per recuperare i dati mentre si aspetta un array. Quando i dati vengono ricevuti ottengo questo errore:

TypeError: value.push is not a function 

C'è un problema con la risposta do da server =?

Fonte di errore:

// jshint +W018 
       if (action.isArray) { 
        value.length = 0; 
        forEach(data, function(item) { 
        if (typeof item === "object") { 
         value.push(new Resource(item)); 
        } else { 
         // Valid JSON values may be string literals, and these should not be converted 
         // into objects. These items will not have access to the Resource prototype 
         // methods, but unfortunately there 
         value.push(item); 
        } 
        }); 
       } else { 
        shallowClearAndCopy(data, value); 
        value.$promise = promise; 
       } 
       } 

Controller:

var stream = []; 
stream = new VideoStream({param: 'streamTypes'}); 
stream.$query(); 

Servizio:

app.service('DataService', [ 
    '$resource', 'Common', '$rootScope', 
    function($resource, Common, $rootScope) { 
     return $resource($rootScope.appWebRoot + "myUrl/:param", {param: '@param'}, 
     { 

     }); 
    } 
]); 

enter image description here enter image description here enter image description here

VideoStream:

app.service('VideoStream', [ 
    '$resource', 'Common', '$rootScope', 
    function($resource, Common, $rootScope) { 
     return $resource($rootScope.appWebRoot + "videoStreams/api/:param", 
     {param: '@param'}, 
     { 

     }); 
    } 
]); 
+0

farci [continua questa discussione in videochat] (http://chat.stackoverflow.com/rooms/ 83452/discussione-tra-Grundy-e-Kaspar). – Grundy

risposta

9

Il problema che hai è che si sta creando un'istanza della risorsa come un oggetto

var stream = []; 
stream = new VideoStream({param: 'streamTypes'}); //This is the problem. $resource is expecting an array. 
stream.$query(); //This is an instance method. 

//All you need to do is: 
var stream = []; 
stream = VideoStream({param: 'streamTypes'}).query(); 

Da https://docs.angularjs.org/api/ngResource/service/$resource:

$ rendimenti risorse:

A resource "class" object with methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:

{ 'get': {method:'GET'}, 
    'save': {method:'POST'}, 
    'query': {method:'GET', isArray:true}, 
    'remove': {method:'DELETE'}, 
    'delete': {method:'DELETE'} }; 

Calling these methods invoke an $http with the specified http method, destination and parameters. When the data is returned from the server then the object is an instance of the resource class. The actions save, remove and delete are available on it as methods with the $ prefix

+0

Questo è stato causato dal fatto che uso .service anziché .factory? – Kaspar

+1

@Kaspar, no, sembra questo perché l'istanza può essere mappata solo a un oggetto – Grundy

+2

Il modo in cui $ resource funziona è che quando si definisce una risorsa $ restituisce una classe con alcuni metodi di classe come query e get. Chiamando questi metodi creerai una nuova istanza della tua classe.Non è necessario "new" la classe, a meno che non si disponga di dati esistenti che si desidera trasformare in una classe di risorse. Maggiori informazioni su https://docs.angularjs.org/api/ngResource/service/$resource – Wawy

1

Inoltre alla risposta Wawy s', secondo la documentazione here:

The action methods on the class object or instance object can be invoked with the following parameters:

  • HTTP GET "class" actions: Resource.action([parameters], [success], error])
  • non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
  • non-GET instance actions: instance.$action([parameters], [success], [error])

sul controllo del valore della VideoStream nel controller, troviamo:

function Resource(value) { 
     shallowClearAndCopy(value || {}, this); 
    } // Actual Resource class object with regular action(s) we can work with 

ci

Calling VideoStream({param: 'streamTypes'}) rendimenti:

undefined // Cannot call anything of undefined 

e new VideoStream({param:'streamTypes'}) rendimenti:

Resource {param: "streamTypes"} 
// Instance of the Resource class (an object) where $action(s) are available 
// Verified in the chrome console 

Con questo in mente, il caso dovrebbe essere:

var stream = []; 
stream = VideoStream.query({param: 'streamTypes'}); 
Problemi correlati