javascript - Ionic TypeError: Cannot read property 'title' of undefined at first time i click save button -
my problem first time click save button error, if click button once again code works fine, can't understand happening why undefined?
here code:
$scope.getprofilecontent = function(value){ words.getprofilecontent(value).then(function(detalle){ if (!detalle) { $scope.profilecontent = { title: null }; }else{ $scope.profilecontent = detalle; } }); }; if ($scope.salvo === true ){ $scope.nosavedpopup(); }else if($scope.editwordrow.title===null || $scope.imguri===null || $scope.tempsrc===null || $scope.tempsrc2===null) { $scope.nullpopup(); console.log($scope.editwordrow.title); console.log($scope.imguri); console.log($scope.tempsrc); console.log($scope.tempsrc2); }else { $scope.getprofilecontent(content); if ($scope.profilecontent.title===null) { console.log("el title es : " + $scope.profilecontent.title); }else{ console.log("el title es : " + $scope.profilecontent.title +"y esta en bd"); } }
services.js
self.getprofilecontent = function(content) { var parameters = [content]; return dba.query("select * learners title = (?)", parameters) .then(function(result) { return dba.getbyid(result); }); };
view.html
<div class="text-center"><input style="font-size: 18px !important; text-align: right !important; font-weight: bold !important;" type="text" ng-model="editwordrow.title"> <hr style="border-color: #11c1f3 !important; margin-right: 20px !important;margin-left: 20px !important;"> </div>
this driving me crazy, regards!!!
you doing wrong, because getprofilecontent
no synchronous function. mixing synchronous , asynchronous calls. getprofilecontent
in services.js returns promise. , handling correctly @ beginning of code:
words.getprofilecontent(value).then(function(detalle){
your scope function synchronous function. reason why profilecontent
undefined is, promise call has not been resolved when call first time. there several options how solve this, suggest make synchronous function async doing:
$scope.getprofilecontent = function(value){ //notice return on next line return words.getprofilecontent(value).then(function(detalle){ if (!detalle) { $scope.profilecontent = { title: null }; }else{ $scope.profilecontent = detalle; } return detalle; }); };
you return promise , in later code able continue when resolved like:
$scope.getprofilecontent(content).then(function(detalle) { if (detalle.title===null) { console.log("el title es : " + $scope.profilecontent.title); }else{ $scope.profilecontent = detalle; console.log("el title es : " + $scope.profilecontent.title +"y esta en bd"); }
you can read intro on angular promises , traps.
Comments
Post a Comment