TypeError:无法读取未定义的属性“0”。茉莉

时间:2017-08-18 23:35:33

标签: javascript jquery html angularjs jasmine

  • 我是js和jasmine的新手
  • 我正试图为我的方法找一个茉莉花测试案例。
  • 但我面临一个错误:TypeError: Cannot read property '0' of undefined.
  • 我尝试了各种方法将数组传递给我的方法体育,但我仍然得到同样的错误。你们能告诉我如何解决它吗?

测试

it('Read player hiking', (done) => {
    let callFirstTime : boolean = true;
    let url=

    spyOn(swimming.playerhikings.movieService,'getResponse').and.
        callFake(() => { 
                if(callFirstTime){
                    callFirstTime = false; // Invoked by detectChanges() 
                    return Observable.of([{
                        "hikingId": "100",
                        "hikingName": "http://localhost:3000/assets/js/actualairings.json",
                        "hikingType": "TITLE",
                        "hikingData": "YWZjYXJlZ2Vyamh2dmFyZWdoYnZi",
                        "notes": "",
                        "notesId": "100",
                        "elfDocID": "100",
                        "url": "http://localhost:3000/upload",
                        "date": "06/27/2017",
                        "addedByName": "Kamal",
                        "userID": "206509786",
                        "operationType": "create"
                      }, {
                        "hikingId": "101",
                        "hikingName": "uploadTest4.txt",
                        "hikingType": "TITLE",
                        "hikingData": "Manish",
                        "notes": "",
                        "notesId": "101",
                        "elfDocID": "101",
                        "url": "http://localhost:3000/upload",
                        "date": "06/27/2017",
                        "addedByName": "Kamal",
                        "userID": "206509786",
                        "operationType": "create"
                      }]
                );
                }
         });

         //const args = ['p0', 'p1', 'p2'];
        //call_me.apply(this, args);

         var fruits = ['Apple', 'Banana'];
         var fruits1 = {"test": "1"};

          //console.log("swimming.playerhikings---->" + 
          //      JSON.stringify(swimming.playerhikings.sports.apply(this, args)));

         //spyOn(swimming.playerhikings.gridkendo,'enableSavehiking').and.returnValue(null);
         //swimming.playerhikings.fileSelect = "text.txt";
         //swimming.playerhikings.sports(fruits[0]);
        swimming.playerhikings.sports(fruits1.test);

         //swimming.playerhikings.coffee( {0: File, length: 1});
         //console.log("swimming.playerhikings._dataSource._data.length---->" + swimming.playerhikings._dataSource._data.length);
         fixture.whenStable().then(() => {
       done();
       //expect(swimming.playerhikings._dataSource._data.length).toEqual(3);
    });

});

代码

sports(inputValue: any): void {
  var that = this;
  var file: File = inputValue.files[0];

  var myReader: FileReader = new FileReader();
  myReader.onloadend = (e) => {
    this.encodeBase64 = myReader.result;
    that.fileSelect = $("#attachhikingBrowseBtn").val().replace(/^.*\\/, "");
    if (that.fileSelect == '') {
      that.dragDrop = that.clearBtn;
    } else {
      that.dragDrop = "";
      that.dragDrop = that.fileSelect;
    }
  }
  $('.addELFhikingForm').show();
  if (inputValue.files.length > 0) {
    var fileSize = 0;

    fileSize = inputValue.files[0].size / 1048576; //size in mb 

    if (fileSize > 5) {
      alert("The hiking size exceeds the max limit of 5 MB");

      //confirm("The hiking size exceeds the max limit of 5 MB");

    }
    myReader.readAsDataURL(file);
  }
}

1 个答案:

答案 0 :(得分:1)

在这里您确切地告诉您错误消息:获取未定义的属性'0':

var fruits1 = {"test": "1"};
swimming.playerhikings.sports(fruits1.test);

sports(inputValue: any): void {
  var that = this;
  var file: File = inputValue.files[0];
                             ^^^^^^ undefined
}