发布于 4年前

Angular JS : Cannot read property 'substring' of undefined

<span class="hljs-variable">$http({
            method : <span class="hljs-string">'GET',
            url : urls,
            <span class="hljs-comment">// 数据类型
            dataType : <span class="hljs-string">"json",
            <span class="hljs-comment">// 要传递的数据
            data : <span class="hljs-string">"",
        }).then(<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">successCallback<span class="hljs-params">(response) {
            <span class="hljs-variable">$scope.credit = response.data;
            <span class="hljs-keyword">var ceditScope=<span class="hljs-variable">$scope.credit.credit_score;
            <span class="hljs-variable">$scope.updateTime=<span class="hljs-string">"更新于:"+response.data.updateTime.substring(<span class="hljs-number">0,<span class="hljs-number">10);         

            <span class="hljs-variable">$scope.change(ceditScope);
            $(<span class="hljs-string">'.thirdline').text(<span class="hljs-variable">$scope.updateTime);
        }, <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">errorCallback<span class="hljs-params">(response) {

        })</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

报此错误原因在于对字符串做substring时,没有判断其是否为空。

按如下方式修改后即可解决:


$http({
            method : <span class="hljs-string">'GET',
            url : urls,
            <span class="hljs-comment">// 数据类型
            dataType : <span class="hljs-string">"json",
            <span class="hljs-comment">// 要传递的数据
            data : <span class="hljs-string">"",
        }).then(<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">successCallback<span class="hljs-params">(response) {
            <span class="hljs-variable">$scope.credit = response.data;
            <span class="hljs-keyword">var ceditScope=<span class="hljs-variable">$scope.credit.credit_score;
            <span class="hljs-keyword">if(!!response.data.updateTime){
                <span class="hljs-variable">$scope.updateTime=<span class="hljs-string">"更新于:"+response.data.updateTime.substring(<span class="hljs-number">0,<span class="hljs-number">10);         
                $(<span class="hljs-string">'.thirdline').text(<span class="hljs-variable">$scope.updateTime);
            }<span class="hljs-keyword">else{
                $(<span class="hljs-string">'.thirdline').text(<span class="hljs-string">"");
            }
            <span class="hljs-variable">$scope.change(ceditScope);
        }, <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">errorCallback<span class="hljs-params">(response) {

        })
©2020 edoou.com   京ICP备16001874号-3