angularjs - Why is data variable undefined in resource query transform -


i new angular...so sure doing wrong. spent hours trying search solution angularjs documentation next useless...and pretty every example out there tries set @ global level.

i trying pass complex javascript object query prameter. object has property array , need flatten (proper term ??) object mvc binding can correctly instantiate model.

the object trying pass along lines of

newrequest = {    searchterms: 'error',    pagesize: 25,    ...    facets: [        { field: 'createdby', value: 'joe' },        { field: 'createdby', value: 'mary' }    ] } 

i declared resource follows

(function () {     "use strict";      angular         .module('common.services')         .factory('searchresource', ['$resource', 'appsettings', searchresource]);      function searchresource($resource, appsettings) {         return $resource(appsettings.searchpath, null, {             query: {                 method: 'get',                 transformrequest: function (data, headersgetter) {                     if (data === undefined) {   // true                         return data;                     }                      return $.param(data);                 }             }         });     } }()); 

and using with

vm.executesearch = function () {     searchresource.query(         newrequest,         function (data) {             vm.response = data;             vm.request = data.request;         }     ); } 

the transformrequest function is being called...and headersgetter has value.

additional info

as suggested, changed direction , instead of using resource went service via factory. same result...the data parameter undefined. here new code.

(function () {     "use strict";      angular         .module('common.services')         .factory('searchprovider', ['$http', 'appsettings', searchprovider]);      function searchprovider($http, appsettings) {         return {             query: function (request, callback) {                 $http({                     url: appsettings.searchpath,                     method: 'get',                     params: request,                     transformrequest: function (data, headersgetter) {                         if (data == undefined) {                             return data;                         }                          return $.param(data);                     }                 })                 .success(function (data) {                     callback(data);                 });             }         }     }   }());  searchprovider.query(     newrequest,     function (data) {         console.log(data);          vm.response = data;         vm.request = data.request;     } ); 

but problem data undefined! know newrequest object valid because call goes out. has improperly formatted url. did go wrong?

the problem it's request , ngresource won't accept data in request:

see https://github.com/angular/angular.js/blob/master/src/ngresource/resource.js#l526

var hasbody = /^(post|put|patch)$/i.test(action.method);

it uses hasbody determine if set data parameter or not.

so @ point best bet move using $http since ngresource more restful api or switch api post instead of get.

edit

and thing: don't need use if (data === undefined) when can : if(!data) means same thing.

javascript thruthiness : https://developer.mozilla.org/en-us/docs/glossary/truthy

reply additional info

you're still getting undefined in data because you're not setting data anything, you're setting params. once you're here don't need transform request.

you can

$http({   ...   params: $.param(request),   ... }) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -