Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I’m looking for support on a query using $stateParams in Angularjs. The query is returning undefined.

I have an array of photos in a news feed. Clicking on a photo should take you to a new page of that photo. I'm below attempting to just spit out the photo id in Network google dev tools tab first.

environment: angularjs, javascript parse api using promises/factory/controller/view

Route

.state(‘gallery.item', {
      url: '/item/:galleryid',
      views: {
        menuContent: {
          controller: 'ItemController as vm',
          templateUrl: path + ‘/galleryitem/item.html'
        }
      }
    })

Factory - Parse query

function getFirst(Class, term1, term2){

        var defer = $q.defer();
        var ParseString = Parse.Object.extend(Class);
        var query = new Parse.Query(ParseString);
         query.equalTo(term1, term2);
         if(Class==='Gallery') {
            query.equalTo('approve', true);
            query.ascending('createdAt');
         }
        query
        .first()
        .then(function(resp){
          console.log('getFirst', resp);
          if (resp === undefined) {
            defer.reject(resp);
          } else {
            defer.resolve(resp);
          }

        });

    return defer.promise;
  }

return {
getFirst: getFirst
}

ItemController

function ItemController($scope, $state, $rootScope, $stateParams, Factory) {
var vm = this;

getItemDetails();

function getItemDetails() {

vm.Gallery = [];

Factory
.getFirst('Gallery', 'objectId', $stateParams.id)
.then(function(resp) {

vm.GalleryData = response;

 console.log(resp);

 vm.Gallery.push({
            id: resp.id,
        });

      });

Any help would be greatly appreciated.

Thanks

Cameron

share|improve this question
up vote 0 down vote accepted

In your routing config you use url: '/item/:galleryid', but in controller you are looking for $stateParams.id which doesn't match routing config

Change to $stateParams.galleryid

share|improve this answer
    
Thank you very much. The query is coming through now. I still can't seem to show it in my view though.? Can I just do <h2>{{ gallery.id }}</h2> in my item.html? – Cameron_Lynch 14 hours ago
    
since Parse is not within aangular context you need to run $scope.$apply() to have angular run digest on view whenever scope is changed externally – charlietfl 14 hours ago
    
Thank you. Its still getting upset. $digest already in progress errors when I run it. From some research, was told to try $scope.$applyAsync();, which makes the errors go away, but data still not binding to view. I'll keep researching. Cheers. – Cameron_Lynch 13 hours ago
    
can also try $timeout() ... have to inject $timeout service in controller to use it – charlietfl 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.