离子类型错误:第一次单击保存按钮时无法读取未定义的属性“标题”
Ionic TypeError: Cannot read property 'title' of undefined at first time i click save button
我的问题是我第一次单击保存按钮时出现该错误,但是如果我再次单击该按钮,代码工作正常,所以我无法理解发生了什么为什么未定义?
这是我的代码:
$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"所属的对象在运行引发异常的代码时未定义。
在尝试检查标题字段之前,您需要确保定义对象的代码已运行。
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- angularjs无法读取未定义的属性then
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- JavaScript承诺-无法读取属性'那么'的未定义
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- Javascript未捕获类型错误:无法读取属性'0'的未定义
- jQuery控制台错误:无法读取属性'top'的未定义
- TypeError:无法读取属性'findAll'的未定义
- 无法读取属性'errcap'未定义的jqgrid
- 无法读取未定义的属性“rid”
- 未捕获的类型错误: 无法读取未定义的属性“图像”
- 可以't读取'getHTML'未定义的Liferay CK Editor内容
- 在node.js中未定义读取属性错误