在express.js项目中发送错误后无法设置标头

时间:2017-08-23 15:37:53

标签: javascript node.js express firebase asynchronous

我正在实现一个创建用户函数,该函数将表单数据发布到firebase。该函数有效,firebase已收到数据,但我的控制台显示以下错误:

错误:发送后无法设置标头。

有时错误不会发生,我在Stack Overflow中找到了不同的解决方案,但它没有工作,我仍然无法弄清楚我的代码的哪个部分存在问题。谁能帮我?

var memberRef = firebase.database().ref("Membership");
//create new student record
function createNewStudentRecord(studnetInfo){
      var newRecord = JSON.parse(JSON.stringify(studnetInfo));
      newRecord["status"] = "student";
      newRecord["create_date"] = getCurrentDate();
      memberRef.push(newRecord);
}


router.get('/studentTable', function(req, res, next) {
  var studentArr = [];
  memberRef.on('value',(snapshot)=>{
  var tableHeading = [];
  var tableContent = [];
  var StudentRecord = {};

  for(var uid in snapshot.val()){
    memberRef.child(uid).on('value',(snapshot)=>{
    var memberObject = snapshot.val();
    if(uid !== null){
      memberObject["uid"] = uid;
    }
    var memberStatus = memberObject["status"];
    if(memberStatus === 'student')
      studentArr.push(memberObject);
    })
  } 

    for(i in studentArr){
      var record = Object.create(StudentRecord);
      delete studentArr[i].password;
      for(props in studentArr[i]){
        record[props] = studentArr[i][props];
      }
      tableContent.push(record);
    }
    res.render("studentTable",{username:user.getUsername(),studentTableContent:tableContent});
  })
});

router.post('/studentTable/createNewEntries', function(req, res, next) {
  createNewStudentRecord(req.body)
  return res.redirect('/dashboard/studentTable');
});

0 个答案:

没有答案