未捕获的TypeError:无法读取未定义的属性“email”

时间:2016-05-27 05:30:29

标签: javascript jquery ajax email

我正在尝试从我的数据库中获取我的课程HTML代码,然后通过电子邮件将其发送给我的用户。我正在使用Rapid API。我的代码检查courseLoop是否与“course”相同,如果为true则它通过 .done 运行并在dataHTMLMain == 1或2或3的日期运行sendEmail()函数,依此类推..

sendEmail()检查是否有任何用户具有courseLoop所在的相同课程。如果是(通过 .done())并将电子邮件发送给用户。一旦将电子邮件发送给用户,它就会将用户每天更新1.这是我遇到问题的地方。

另外,如果我手动制作它:

- “email”:dataMain [0] .email,
  - “day”:( parseInt(dataMain [0] .day)+ 1),

它有效。

我得到的错误是:

  

sendEmail.js:135 Uncaught TypeError:无法读取未定义的属性“email”

我该如何解决这个问题?感谢。

sendEmail.js

var courses = ["course1", "course2", "course3", "course4", "Lorem Ipsum - Decoded"];

var courseLoop;
var dataMain;
var dataHTMLMain;
var EMAILcontentDay1,EMAILcontentDay2,EMAILcontentDay3,EMAILcontentDay4,EMAILcontentDay5,EMAILcontentDay6,
EMAILcontentDay7,EMAILcontentDay8,EMAILcontentDay9,EMAILcontentDay10;

var userTime;
var d = new Date();
var utc = d.getUTCHours();
var timeInAmerica = utc - 5;
var timeInAfrica_Europe = utc + 3;
var timeInAsia_Australia = utc + 8;

for(var i=0;i<courses.length;i++){
  courseLoop = courses[i];

/*GETTING EMAIL CONTENT*/
  jQuery.ajax({
    url: "http://calm-cheetah-mwqd.rapidapi.io/get-email-content",
    type: "POST",
    contentType: "application/x-www-form-urlencoded",
    data: {
      "course": courseLoop,
    },
  })
  .done(function(data, textStatus, jqXHR) {
    console.log("HTTP Request Succeeded: " + jqXHR.status);
    dataHTMLMain = data;
    for(var j=0; j<dataHTMLMain.length; j++){
      if(dataHTMLMain[j].day == 1){
        EMAILcontentDay1 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "1", EMAILcontentDay1);
      }
      else if(dataHTMLMain[j].day == 2){
        EMAILcontentDay2 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "2", EMAILcontentDay2);
      }
      else if(dataHTMLMain[j].day == 3){
        EMAILcontentDay3 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "3", EMAILcontentDay3);
      }
      else if(dataHTMLMain[j].day == 4){
        EMAILcontentDay4 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "4", EMAILcontentDay4);
      }
      else if(dataHTMLMain[j].day == 5){
        EMAILcontentDay5 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "5", EMAILcontentDay5);
      }
      else if(dataHTMLMain[j].day == 6){
        EMAILcontentDay6 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "6", EMAILcontentDay6);
      }
      else if(dataHTMLMain[j].day == 7){
        EMAILcontentDay7 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "7", EMAILcontentDay7);
      }
      else if(dataHTMLMain[j].day == 8){
        EMAILcontentDay8 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "8", EMAILcontentDay8);
      }
      else if(dataHTMLMain[j].day == 9){
        EMAILcontentDay9 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "9", EMAILcontentDay9);
      }
      else if(s[j].day == 10){
        EMAILcontentDay10 = dataHTMLMain[j].HTML;
        sendEmail(dataHTMLMain[j].course, "10", EMAILcontentDay10);
      }
      /*else if(dataHTMLMain[j].day == 10){
        EMAILcontentDay10 = dataHTMLMain[j].HTML;
      }*/

}
  })
  .fail(function(jqXHR, textStatus, errorThrown) {
    console.log("HTTP Request Failed");
  })
  .always(function() {
    /* ... */
  });
}
/*END GETTING EMAIL CONTENT*/

/*SENDING OUT THE EMAILS*/
function sendEmail(course, day, content){
  jQuery.ajax({
    url: "http://calm-cheetah-mwqd.rapidapi.io/get-day",
    type: "POST",
    contentType: "application/x-www-form-urlencoded",
    data: {
      "course": course,
    }
  })
  .done(function(data, textStatus, jqXHR) {
    dataMain = data;
    for(var j=0; j<dataMain.length; j++){

//  if(userTime == 24){
    if(dataMain[j].day == day){
      jQuery.ajax({

        url: "http://calm-cheetah-mwqd.rapidapi.io/send-emails-main",
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        data: {
          "email": dataMain[j].email,
          "course": course,
          "HTML": content,
        },
      })
      .done(function(data, textStatus, jqXHR) {
        console.log("HTTP Request Succeeded: " + jqXHR.status);
        console.log(data);
        //console.log(dataMain[j].email)


              jQuery.ajax({
                url: "http://calm-cheetah-mwqd.rapidapi.io/update-day",
                type: "POST",
                contentType: "application/x-www-form-urlencoded",
                data: {
                  "email": dataMain[j].email, //problem!
                  "day": (parseInt(dataMain[j].day) + 1), //problem!
                },
              })
              .done(function(data, textStatus, jqXHR) {
                console.log("HTTP Request Succeeded: " + jqXHR.status);
                console.log(data);
              })
              .fail(function(jqXHR, textStatus, errorThrown) {
                console.log("HTTP Request Failed");
              })
              .always(function() {
                /* ... */
              });


      })
      .fail(function(jqXHR, textStatus, errorThrown) {
        console.log("HTTP Request Failed");
      })
      .always(function() {
        /* ... */
      });

    }





//  } //time check end
  }

})

  .fail(function(jqXHR, textStatus, errorThrown) {
    console.log("HTTP Request Failed");
  })
  .always(function() {
    /* ... */
  });

}

0 个答案:

没有答案