我已经看过以前回答的问题,但似乎无助于我的情况。
尝试使用angular从数据库中检索信息时出现此错误。
angular.js:10071 TypeError: Cannot read property 'Result' of undefined
at services.js:29
我知道我正在尝试重新定义一个变量。但不知道如何解决它。这是我的 services.js 代码。
(function (){
//turn strict syntax on
"use strict";
angular.module('ContinentApp').
service('dataService',
[
'$q',
'$http',
function ($q, $http){
//create variable to hold the urlBase data
var urlBase = '/cm0665-assignment/server/index.php';
// retrieve promise for the success method to get continents
this.getContinents = function(){
var defer = $q.defer(),
data = {
action: 'list',
subject: 'continents'
}; //end of data array object
// create an ajax call that chains success and error
// which will accept or reject the promise
$http.get(urlBase, {params: data, cache: true}).
//if successful then get the promise
success(function(response){
defer.resolve({
//create a data property from the values of the response
data: response.ResultSet.Result,
//create rowCount property from response values
rowCount: response.ResultSet.RowCount
});//end of defer resolve
}). //end of success response
//error function to reject the promise
error(function (err){
defer.reject(err);
}); // end off error function
//get the deferred promise
return defer.promise;
}; // end of getContinent
//function to get the countries from the DB
// similar to the getContinent function
this.getCountry = function(A2Code){
// create a data array object and store in variable
var defer = $q.defer(),
data = {
action: 'list',
subject: 'country',
id: A2Code
};
// create an ajax call that chains success and error
// which will accept or reject the promise
$http.get(urlBase, {params: data, cache: false}).
//if successful then get the promise
success(function(response){
defer.resolve({
//create a data property from the values of the response
data: response.ResultSet.Result,
//create rowCount property from response values
rowCount: response.ResultSet.RowCount
});
}). // end of function success
//error function to reject the promise
error(function(err){
defer.reject(err);
});
//get the deferred promise
return defer.promise;
};
// create function to update country
//similar toi the two above
this.updateCountry = function (country) {
var defer = $q.defer(),
// create data array object
data = {
action: 'update',
subject: 'country',
data: angular.toJSON(country)
};
// create an $http POST so data can be updated
$http.post(urlBase, data).
success(function(response){
defer.reject(response);
}).
error(function(err){
defer.reject(err);
});
return defer.promise
};
} // end of function $q, $http
]
). // end of service
service('applicationData',
function($rootScope){
var sharedService = {};
sharedService.info = {};
sharedService.publishInfo = function (key, obj) {
this.info[key] = obj;
$rootScope.$broadcast('systemInfo_' +key, obj);
};
return sharedService;
}
);//end of service
}());// end of IIFE
这是一个连接到它的JSONRecordSet文件
<?php
if ($_SERVER['HTTP_HOST'] == 'localhost') {
ini_set('display_errors', true);
error_reporting(-1); // show all errors
} else {
ini_set('display_errors', false);
error_reporting(0); // show no errors
}
require_once ('classes/pdoDB.class.php');
abstract class R_RecordSet {
protected $db;
protected $stmt;
function __construct (){
$this->db = pdoDB::getConnection();
}
//using two variables $sql and $params to connect to the recordset and
// build an optional associative array prepared statement.
function getRecordSet($sql, $params = null){
//if recordSet is an array then
if (is_array($params)){
//prepare statement to
// pass the name placeholder and the value it'll have
$this->stmt = $this->db->prepare($sql);
// execute the prepared statement
$this->stmt->execute($params);
} //end of if
// query the sql and display the query to show there is an error
else{
$this->stmt = $this->db->query($sql);
}// end of else
return $this->stmt;
}// end of function getRecordSet
}//end of recordSet class
// class created to return records as an encoded JSON string
class JSONRecordSet extends R_RecordSet {
//retrieve the recordSet class so it can be displayed in JSON
function getRecordSet($sql, $elementName = 'ResultSet', $params = null)
{
//store the record set into a variable to create an sql statement
operand
$stmt = parent::getRecordSet($sql, $params);
//fetchAll associated Records via pdo::FETCH_ASSOC
$recordSet = $stmt->fetchAll(PDO::FETCH_ASSOC);
//store all $record set into a variable which collects however many there are
$nRecords = count($recordSet);
//if there are no records then don't return any data
if ($nRecords == 0){
//create variables to use in the else statement and the return statement
$status = 'error';
$message = array("text" => "No Records Found");
$result = '[]';
}
// else return the data from request stream the return the $recordSet
else{
//using the variables created ain the if statement
$status = 'ok';
$message = array("text" => "");
$result = $recordSet;
}
// return JSON data in the following order using json_encode
return json_encode(
array(
//using the variables from the if else staments to return the data as JSON
'status' => $status,
'message'=> $message,
"$elementName" =>array(
"RowCount" =>$nRecords,
"Result" =>$result
)
), //print out in correct json format while checkking the count
JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT
);//end of json encode
} // end of getRecordSet
}//end of JSONRecordSet
这是我的控制器文件
//a page to act as a multi controller
(function(){
//Tell angular to use strict
"use strict";
//build up an index controller to control what is seen on the index.html page
angular.module('ContinentApp').
controller('IndexController',
[
'$scope',
'applicationData', //angular variable as a string
function ($scope, appData) {
//add a title property which we can refer to in our view (index.html)
$scope.title = 'Continents and Country list';
$scope.subtitle = 'Please Select a Continent';
$scope.ID = '';
// turning on a listener for the service $broadcast
$scope.$on('systemInfo_continent', function(e, continent){
$scope.Name = continent.Name;
});// end of $broadcast listener
}
]
). //end of index controller
//create a continent controller
controller('ContinentController',
[
'$scope',
'dataService',
'applicationData',
'$location',
function ($scope, dataService, appData, $location){
appData.publishInfo('continents', {});
var getContinents = function (){
dataService.getContinents().then(
function (response) {
$scope.continentCount = response.rowCount + ' continents';
$scope.continents = response.data;
},
function (err){
$scope.status = 'unable to load data' + err;
},
function (notify){
console.log(notify);
}
); //end of then
}; //end of getContinent
var continentInfo = $location.path().substr(1).split('/');
if (continentInfo.length===2){
// use the ID from the path and assign it to the selected
//continent so it will remain highlighted after a page reload
$scope.selectedContinent={ID: continentInfo[1]};
}
//return the ID and concatenate the continentInfo
$scope.selectedContinent = {};
$scope.selectContinent = function($event, continent){
$scope.selectedContinent = continent;
$location.path('/continents/', continent.ID);
appData.publishInfo('continent', continent)
};
// get the continent, as defined above
getContinents();
}
]
). // end of continent controller
controller('CountryController',
[
'$scope',
'dataService',
'$routeParams',
function ($scope, dataService, $routeParams){
$scope.country = [];
$scope.countryCount =0;
var getCountry=function (ID){
dataService.getCountry(ID).then(
function (response){
$scope.countryCount=response.rowCount + ' country';
$scope.country = response.data;
},
function (err){
$scope.status = 'unable to load data'+err;
}
);// end of then
};
//only if a continent ID was passed, then get the countries
if ($routeParams && $routeParams.ID){
console.log($routeParams.ID);
getCountry($routeParams.ID);
}
$scope.showEditCountry=function ($event, country, editorID){
var element = $event.currentTarget,
padding = 22,
posY = (element.offsetTop + element.clientTop + padding)-(element.scrollTop + eleementclientTop),
studentEditorElement = document.getElementByID(editorID);
console.log(country);
$scope.selectedCountry = angular.copy(country);
$scope.editoeVisible = true;
countryEditorElement.style.position = 'absolute';
studentEditorElement.style.top = posY +'px';
}; //end of showEditCountry
// abandon the edit process
$scope.abandonEdit = function (){
$scope.editorVisible = false;
$scope.selectedCountry = null;
}; //end of abandonEdit
// function to save the edited country data
$scope.scope.saveCountry = function(){
var n,
cCount = $scope.country.length,
currentCountry;
$scope.editorVisible = false;
// call the dataService method
dataService.updateCountry($scope.selectedCountry).then(
function (response) {
$scope.status = response.status;
if (response.status === 'ok'){
for (n = 0; n < cCount; n += 1){
currentCountry = $scope.country[n];
if (currentCountry.countryID === $scope.selectedCountry.countryID){
$scope.country[n] = angular.copy($scope.selectedCountry);
break;
}
}
}
console.log(response);
// reset selectedCountry
$scope.selectedCountry = null;
},
function (err) {
$scope.status = "error with save" + err;
}
);//end of then
};// end of saveCountry
} //end of function ($scope, dataService, $routeParams)
]
); // end of CountryController
}()); // end of the instantly initiated function and page