让 AngularJS 与 AWS DynamoDB 一起工作

Getting AngularJS to work with AWS DynamoDB

我能够很好地从 DynamoDB 收集数据,但是当我尝试将数据传递给具有 AngularJS 服务的控制器时,出现未定义类型错误。

// 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
      }


    }
  }
});

我也可以手动创建一个对象,它在功能上等同于我从 DynamoDB 中提取的对象,我工作得很好。

var nestObject = { 
  "company": { "S" : "ACME"},
  "email" : {"S" : "jrwhip@email.com"},
  "firstName" : {"S" : "Jared"},
  "lastName" : {"S" : "Whipple"},
  "phone" : {"S" : "435.494.3333"},
  "repNum" : {"N" : 12164}
};

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

如何从DynamoDB获取数据传递 Table 姓名:ximoRepList

在您将使用 couchDb APIAngular Service 中,我建议使用 Angular $q promise chain logic。这有助于无缝绑定解决方案,也有助于解决以后的用户体验问题。

尝试类似的操作。

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;
            }
        }
    });