离子类型错误:第一次单击保存按钮时无法读取未定义的属性“标题”

Ionic TypeError: Cannot read property 'title' of undefined at first time i click save button

本文关键字:未定义 读取 属性 标题 错误 类型 第一次 按钮 保存 单击      更新时间:2023-09-26

我的问题是我第一次单击保存按钮时出现该错误,但是如果我再次单击该按钮,代码工作正常,所以我无法理解发生了什么为什么未定义?

这是我的代码:

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

服务.js

 self.getProfileContent = function(content) {
    var parameters = [content];
    return DBA.query("SELECT * FROM learners WHERE title = (?)", parameters)
      .then(function(result) {
        return DBA.getById(result);
      });
  };

查看.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>

这让我发疯,问候!!

你做错了,因为getProfileContent不是同步函数。您正在混合同步和异步调用。 getProfileContent在您的服务中.js返回一个承诺。并且您在代码开始时正确处理了它:

Words.getProfileContent(value).then(function(detalle){

但是,您的作用域函数是一个同步函数。因此,profileContent未定义的原因是,当您第一次调用它时,您调用的承诺尚未解决。现在有几种选择可以解决这个问题,我建议您通过以下方式使同步函数异步:

$scope.getProfileContent = function(value){
  //notice the return on the next line
  return Words.getProfileContent(value).then(function(detalle){
    if (!detalle) {
      $scope.profileContent = {
        title: null
      };  
    }else{
      $scope.profileContent = detalle;
    }
    return detalle;
  });   
};

然后,您将返回一个承诺,并且在以后的代码中,只有在解析后才能继续,如下所示:

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

您可以阅读有关角度承诺和陷阱的介绍。

字段"title"所属的对象在运行引发异常的代码时未定义。

在尝试检查标题字段之前,您需要确保定义对象的代码已运行。