1

My data object does not display in html!

I am using parse.com and when I console.log(results) i can drill down and see my object attributes in the inspection window.

app.controller('membershipCtrl', ['$scope', function($scope) {
  var membership = Parse.Object.extend("Membership");
  var query = new Parse.Query(membership);
  query.ascending("priceDate").greaterThan('priceDate', currentTime).limit(1);
  query.first({
    success: function(results) {
      $scope = results;
      console.log($scope);
    },
    error: function(error) {
      alert("Error: " + error.code + " " + error.message);
    }

  });
}]);

From the inspection window:

attributes: Object

price: 90

priceDate: Mon Feb 01 2016 11:17:00 GMT-0700 (MST)

priceDescription: "Thru Sept 2016"

My html is :

<div ng-app="myApp" ng-controller="membershipCtrl">
    {{price}}{{priceDescription}}
</div>
4
  • I am not certain, but it might be because instead of assigning the result's entries to attributes on the $scope, you are overwriting the $scope. This could cause the app to no longer recognize the $scope as an instance of the correct prototype chain. Try $scope.price = results.price; $scope.priceDescription = results.priceDescription; and see if that works. Commented Jan 18, 2016 at 4:38
  • I have added your suggestions and when i 'console.log($scope)' the price and descriptions show as undefined?? Commented Jan 18, 2016 at 15:29
  • Where did you add the console.log? Commented Jan 18, 2016 at 19:10
  • at the end of success Commented Jan 18, 2016 at 19:29

4 Answers 4

2

Your success function is overwriting the whole $scope variable instead of placing values in it. You need to do the latter in order for anything to actually be accessible to your view:

success: function(results) {
    $scope.price = results.price;
    $scope.priceDescription = results.priceDescription;
    console.log($scope.price);
    console.log($scope.priceDescription);
},
Sign up to request clarification or add additional context in comments.

2 Comments

when i use your suggestion i see undefined with 'code' console.log($scope.data); 'code'
@MorganHayes That console.log line was a mistake. Fixed it now.
1

UPDATE: Find working version of your fiddle here: https://jsfiddle.net/h4od21fs/1/

So you have number of issues here:

  • as others have said Parse and angular doesn't know about each other without angular plugin, so in your case you need to call $scope.$apply() after you got data

  • you have handlebars plugged in together with angular and have total mess with layout

  • you can't just use object that parse returns you, it returns you active record so you need to call methods to get actual data from it , i.e. :

    vm.results.price = results.get('price')

  • and in your fiddle you did not include angular.js, but included a lot of other stuff , like bootstrap, handlebars and jquery.

  • also don't forget to change key in parse.com so no one would use your quotas.

Resulting code:

var app = angular.module('myApp',[]);

app.run(function() {
  Parse.initialize("Clfupzjbt9iWIdezPCZuBJqajuxHJ8okP5nteViS", "sakt1qjwM4duTAo3ZCvWWBM32Tv3ZdL13PQ0Eea4");

});

var currentTime = new Date();
currentTime = new Date(currentTime.getTime() - 25200000);

app.controller('membershipCtrl', ['$scope', function($scope) {
  var vm = this;
  vm.results = null; 
  var membership = Parse.Object.extend("Membership");
  var query = new Parse.Query(membership);

  //query.ascending("priceDate").greaterThan('priceDate', currentTime).limit(1); 
  // i commented it out as it was not returning any data with those conditions

  query.first({
    success: function(results) {
      vm.results = {
        price  : results.get('price'),
        priceDescription: results.get('priceDescription')      
      };
     $scope.$apply();
     console.log(vm);
    },
    error: function(error) {
      alert("Error: " + error.code + " " + error.message);
    }

 });
 }]);

When you are doing $scope = smooth inside controller you are not changing scope, but rather changing variable assigned to $scope argument of your controller function. As it is passed as reference, it would not affect $scope object that was passed into controller.

You should add property on the scope instead, or to controller itself if you are using ControllerAs syntax, i.e. for $scope:

$scope.results = results  // inside your success callback

For controllerAs syntax:

app.controller('membershipCtrl', ['$scope', function($scope) {
  var vm = this;
  var membership = Parse.Object.extend("Membership");
  var query = new Parse.Query(membership);
  query.ascending("priceDate").greaterThan('priceDate', currentTime).limit(1);
  query.first({
    success: function(results) {
      vm.results = results;
      console.log(vm);
    },
    error: function(error) {
     alert("Error: " + error.code + " " + error.message);
    }

 });
 }]);

And in html:

<div ng-app="myApp" ng-controller="membershipCtrl as vm">
   {{vm.results.price}}{{vm.results.priceDescription}}
</div>

7 Comments

I have used your code and i can drill down in the console and see the attributes of "vm". nothing is displaying on the page. i also added some regular text to within the <div> just to test that something would display and the text is displaying. what do you think?
@MorganHayes can you create jsfiddle or plunkr with your code?
I signed up for jsfiddle and added my code. I do not see how to add it to my public dashboard?
just paste url in your question here. it is publicly available
@MorganHayes see my updated answer and updated fiddle
|
0

From what I can tell, you are overwriting the entire $scope object, which has certain properties you need to maintain to keep your Angular application running correctly and keep the bindings intact.

What you will want to do is store the returned values as properties on the $scope object. Instead of $scope = results; you should do a few separate declarations:

$scope.price = results.price;
$scope.priceDescription = results.priceDescription;

...and so on for any properties you want to reference in your HTML template.

Remember that $scope is not just a store of values, but an object that is built to play nicely within the context of the rest of the Angular framework. If you want to see it's unique properties, console.log($scope) before you overwrite it (or after you fix you declarations to the pattern described above).

Comments

0

Several problems are:

Parse is not part of angular core so you need to tell angular to update the view when making scope changes.

You should assign the data to properties of scope, not overwrite the scope object. A simple way to do that is angular.extend() which will update $scope with the same properties and values as in your results object

query.first({
    success: function(results) {
      angular.extend($scope,results);// merge results into scope
      $scope.$digest();//notify angular to run digests
      console.log($scope.priceDescription);// check one of the new properties added
}

1 Comment

i have copied and pasted your success code and get undefined in the console??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.