我有从数据库响应中获得的对象数组。 每个对象都有名称,课程名称,课程,时间,日期。 在我的数据库中它是一对多的关系 - >意味着每个讲座都有很多课程,所以我有一个讲座和时间表。
时间表具有讲话的foreign_key,这意味着 讲座表:lecture_id(primary_key)+更多属性。 times table:id_lecture(foreign_key),id_time(primary_key)+更多属性。 我做了一个查询
“SELECT * from lectures,Times where id_lecture = lecture_id”;
它工作正常,并给我我想要的结果。 问题是每个讲座都有一些课程,我想在同一个离子项目中显示这些课程并将其发送给一个对象。 换句话说,我想这样做。
这是我获取数据的代码
this.coursesData.LoadLectures()
.subscribe(LectureList=> {
console.log("getLec1");
LectureList.forEach(lecture=>{
console.log(lecture);
var newLecture={
name:lecture.name,
email:lecture.email,
phone:lecture.phone ,
lesson_type:lecture.lesson_type ,
detailsHours:{'start_time':lecture.start_time,'end_time':lecture.end_time,'class':lecture.class, 'day':lecture.day},
details:lecture.details,
course_name:lecture.course_name
}
this.coursesA.push(newLecture);
this.coursesNames.push(lecture.course_name);
this.LecturesNames.push(lecture.name);
});
//this.courseSelect=this.coursesNames[0];
this.coursesAD=this.coursesA;
this.coursesNames = this.coursesNames.filter(function(elem, index, self) {
return index == self.indexOf(elem);
})
// console.log("co"+this.coursesA);
this.loader.dismiss();
//console.log(this.LecturesNames);
// return this.coursesA;
},err=>{
console.log(err);
});
}
你可以看到newLecture包含
var newLecture={
name:lecture.name,
email:lecture.email,
phone:lecture.phone ,
lesson_type:lecture.lesson_type ,
detailsHours:{'start_time':lecture.start_time,'end_time':lecture.end_time,'class':lecture.class, 'day':lecture.day},
details:lecture.details,
course_name:lecture.course_name
}
但是有许多课程,所以如果我这样做是因为它会复制名称。
我试过这种方式,但它显示我的错误
public Lectures(){
console.log("getLec");
this.courseSelect='';
this.coursesNames=[];
this.LecturesNames=[];
this.coursesA=[];
this.coursesData.LoadLectures()
.subscribe(LectureList=> {
console.log("getLec1");
LectureList.forEach(lecture=>{
console.log(lecture);
var newLecture={
id:lecture.id_lecture,
name:lecture.name,
email:lecture.email,
phone:lecture.phone ,
lesson_type:lecture.lesson_type ,
detailsHours:{'start_time':lecture.start_time,'end_time':lecture.end_time,'class':lecture.class, 'day':lecture.day},
details:lecture.details,
course_name:lecture.course_name
}
console.log("de"+newLecture.detailsHours.start_time);
if(!newLecture.id){
this.coursesA[newLecture.id]=newLecture;
// this.coursesNames.push(lecture.course_name);
//this.LecturesNames.push(lecture.name);
}
else{
this.coursesA[newLecture.id].push(newLecture);
}
});
this.coursesAD=this.coursesA;
//this.courseSelect=this.coursesNames[0];
console.log(this.coursesA);
/*this.coursesNames = this.coursesNames.filter(function(elem, index, self) {
return index == self.indexOf(elem);
})
*/
// console.log("co"+this.coursesA);
this.loader.dismiss();
console.log("lets"+this.LecturesNames);
// return this.coursesA;
},err=>{
console.log(err);
});
}
我收到了这个错误
有人有办法解决吗?TypeError:无法读取未定义
的属性'push'
答案 0 :(得分:0)
我认为您可以使用子阵列按名称(或course_name)对课程进行分组。您可以将其newLecture
推入courseA
,而不是在courseA[newLecture.name]
内推送{
"JAVA Course": [
{ lecture1 },
{ lecture2 }
}],
"JS Course": [
{ lecture3 },
{ lecture4 }
}]
}
。
因此,你会有类似的东西:
<ion-item ng-repeat="(key, value) in obj">
<p>{{key}}</p> <!-- example: JAVA Course -->
<div ng-repeat="(subkey, subvalue) in value">
{{subvalue}} <!-- example: lecture hours -->
</div>
</ion-item>
然后,在您的模板中,您可以这样做:
from colander import SchemaNode as SchemaNodeNoNull
class _SchemaNode(SchemaNodeNoNull):
nullable = True
def __init__(self, *args, **kwargs):
# if this node is required but nullable is not set, then nullable is
# implicitly False
if kwargs.get('missing') == required and kwargs.get('nullable') is None:
kwargs['nullable'] = False
super(_SchemaNode, self).__init__(*args, **kwargs)
def deserialize(self, cstruct=null):
if cstruct == '':
if not self.nullable:
raise Invalid(self, _('Cannot be null'))
if self.validator:
self.validator(self, cstruct)
return None # empty string means explicit NULL value
ret = super(_SchemaNode, self).deserialize(cstruct)
return ret