TypeError:无法读取属性' pushObject'未定义的

时间:2017-07-15 22:23:00

标签: javascript jquery ember.js

在我提供任何代码片段之前,我想解释一下我必须实现的任务。

首先,每个班级都有很多科目,每个班级都有很多学生。每个科目和学生都属于一个班级。此外,每个科目和学生都有很多成绩。但是一个年级属于一个学科和一个学生。

我想首先在控制器中检查是否有一个等级与这些参数(学生和主题),然后如果有该等级,只需更新标记属性(类型'数组'我有在app / transforms)。所以这里是控制器app / controllers / overviewofstudents.js:



import Ember from 'ember';

export default Ember.Controller.extend({
  showS: false,
  mark: "",

  actions:{
    showSubjects(){
      this.toggleProperty('showS');
    },

    insertMark(val, val2){
      var subject2 = this.get('store').peekRecord('subject', val2);
      var student = this.get('store').peekRecord('student', val);
      var mark2 = this.get('mark');

      this.get('store').query('grade', {
        filter: {
          student: student,
          subject: subject2
        }
      }).then(function(mark){
        if(mark == null){
          this.get('store').createRecord('grade', {
            marks: [mark2],
            student: student,
            subject: subject2
          }).save().then(function(grade){
            student.get('marks').pushObject(mark2);
            student.save();
            subject2.get('grades').pushObject(mark2);
            subject2.save();
          });
        }else{
          mark.get('marks').pushObject(mark2);
          mark.save();
        }
      });
    }
  }
});




这是app / templates / overviewofstudents.hbs:



<br> <br> <br> <br> <br>
<center>

<table>

<h2> Chosen class: {{model.name}} </h2>

<br>

<i> Current students in class: </i> <br>

<ol> 
{{#each model.students as |student|}}
  <li> {{student.firstName}}  {{student.lastName}} </li>
  <button class="choose" {{action 'showSubjects'}}> Show subjects </button>
  {{#if showS}}
    <br> Current subjects in class: 
    {{#each model.subjects as |subject|}}
      <br> <b> {{subject.title}} </b>
       Choose grade: 
      <select onchange={{action (mut mark) value="target.value"}}>
        <option value="1"> 1 </option>
        <option value="2"> 2 </option>
        <option value="3"> 3 </option>
        <option value="4"> 4 </option>
        <option value="5"> 5 </option>
      </select>
      <br>
      <button {{action 'insertMark' student.id subject.id}}> Insert mark for student </button>
    {{/each}}    
  {{/if}}
{{else}}
  <b> Currently there are no students for the class! </b>
{{/each}}
</ol>

</table>

</center>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

所以你正在做query,它返回一个Promise,它使用AdapterPopulatedRecordArray进行解析,扩展RecordArray,扩展ArrayProxy,你可以找到所有可枚举的方法您可以使用。 您需要使用结果的firstObject,您可以在下面的代码中找到

insertMark(val, val2) {
    var subject2 = this.get('store').peekRecord('subject', val2);
    var student = this.get('store').peekRecord('student', val);
    var mark2 = this.get('mark');

    this.get('store').query('grade', {
        filter: {
            student: student,
            subject: subject2
        }
    }).then(function(mark) {
        if (mark == null) {
            ...
        } else {
            //Here mark is AdapterPouplatedRecordArray. so please confirm are you receiving the result.
            console.log(' Mark ', mark);
            //You need to use firstObject
            mark.get('firstObject').get('marks').pushObject(mark2);
            mark.get('firstObject').save();
        }
    });
}

如果此查询将始终返回单个记录,那么您可以尝试queryRecord而不是查询。为此,您需要实现后端以接受并返回响应。