1

I am able to gather data just fine from DynamoDB, but I am getting a TypeError undefined when I try to pass the data to a controller with an AngularJS service.

// Query for Replicated Site Using Name
coachdb.query(param, function(err,data) {
  if (err) {
    console.log(err, err.stack); // an error occurred
  } else {
    if (data.Items.length > 0) {
      //console.log(data); // THIS IS LOGGING DATA

            var x = (data);
            var y = x.Items[0];
            console.log(x.Items[0]); // THIS LOGS DATA
            console.log(y);          // THIS LOGS THE SAME DATA

            this.coach = function() {
                return y;              // THIS SEEMS TO BE WHERE MY ERROR IS COMMING FROM.
            }

      if ("firstName" in x.Items[0]) {
        var firstName = x.Items[0].firstName.S;
        alert(firstName); // THIS PRODUCES AN ALERT THAT WORKS
      }


    }
  }
});

I can also manually create an Object, which is functially equivalent to what i'm pulling from DynamoDB and I that works just fine.

var nestObject = { 
  "company": { "S" : "ACME"},
  "email" : {"S" : "[email protected]"},
  "firstName" : {"S" : "Jared"},
  "lastName" : {"S" : "Whipple"},
  "phone" : {"S" : "435.494.3333"},
  "repNum" : {"N" : 12164}
};

this.coach = function() {
  return nestObject;
}

How can I get data from DynamoDB to pass Table Name: ximoRepList

2
  • does this have anything to do with dynamodb or couchdb? what is coachdb?? Commented Sep 9, 2016 at 13:41
  • No, it's just a badly named parameter Commented Sep 11, 2016 at 0:24

1 Answer 1

1

In your Angular Service that you will be using the couchDb API, I would recommend using Angular $q promise chain logic. This helps with a seamless binding solution, helps for UX concerns later as well.

Try something similar to this.

angular.module('MyApp', [])
    .controller('MyController', function($scope, MyService) {
        $scope.someDataFromCouchDb = MyService.getSomeData();
    })
    .factory('MyService', function($q) {
        return {
            getSomeData: function() {
                var deferred = $q.defer();

                couchdb.query(param, function(err, data) {
                    if (err) {
                        console.log(err, err.stack);

                        deferred.reject(err);
                    } else {
                        if (data.Items.length > 0) {
                            console.log(data);

                            // return the data
                            // you could also pre-process it before you return it
                            deferred.resolve(data);
                        }
                    }
                });

                return deferred.promise;
            }
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

That worked. The data is now being pulled into the controller. How do you work with states in Angular?

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.