如何在一个get请求中创建数据和呈现

时间:2017-06-26 07:04:52

标签: node.js express mongoose

dashboardRouter.route('/:Teachersubjects/:Class/:Salary')
         .get(function(req,res)
            {
              function handleErr(err,redir)
  {
    if(redir){
      res.end("err");
    }
    else{
      res.writeHead(200, {"content-type": "text/plain"});
      res.end('Error occured');
    }
  }
              Verify.verifySchoolUser(req,res,function(err,schooluserId){
              if(err){
                res.redirect('/schoolusers/login');
                return;
              }
              else{
                SchoolUser.findOne({_id:schooluserId}).exec(function(err,schooluser){
                  if(err)
                  {
                    console.log("NOUSER school")
                    handleErr(err);
                    return;
                  }
                  else
                  {    
                    if(schooluser.count<4){
                      console.log(req.params.Class);
                      vacancies.create(req.params,function(err,vacancy){
                        if(err){
                          console.log(err.message);
                          console.log(err.name);
                          console.log("create error");
                          for (field in err.errors) {
                            console.log(err.errors[field].message); 
                          }
                          handleErr(err);
                            return;
                          }
                          console.log(vacancy);
                  
                          schooluser.vacancies.push(vacancy._id);
                          schooluser.save(function(err){
                          if(err){
                            console.log(JSON.stringify(err));
                            handleErr(err);
                          }
                          teacherforms.find({}, function (err, teacherforms) {
                            if (err) throw err;
                            else { 
                              var teacherforms = teacherforms;
                              console.log(teacherforms);
                              var arr=[];
                              for(i=0; i<teacherforms.length; i++ ) {
                                arr.push(teacherforms[i]._id);
                                console.log(arr);
                              }
                              teacherusers.find({checking:{$in:arr}}, function (err, teacherusers){
                                if (err) throw err;
                                else{
                       
                                  var data =[];
                                  for(i=0; i<teacherusers.length; i++ ) {
                                    var kdata={};
                                    var str = "";
                                    kdata.firstname = teacherusers[i].firstname;
                                    kdata.profilepic= teacherusers[i].profilepic;
                                    kdata.experience= teacherforms[i].Teachingexperience;
                                    var len= teacherforms[i].Qualifications.length;
                                    console.log(teacherforms[i].Qualifications);
                                    for(j=len-1;j>=0;j--){
                                      if(j==0){
                                        str = str + teacherforms[i].Qualifications[j];
                                      }
                                      else{
                                        str=str + teacherforms[i].Qualifications[j] + ", "; 
                                      }
                                    }
                                    kdata.qualifications = str;
                                    data.push(kdata);
                                  }
                                  data.push(vacancy.Class);
                                  //data.push(vacancy.Qualification);
                                  data.push(vacancy.Teachersubjects);
                                  console.log(data);
                                  res.render('../views/dashboard/dashboard_resumes.ejs',{
                                    sdata:data
                                  });
                                }          
                              });
                            }
                          })
                        }); 
                      })
                }
                else {
                res.send("Limit Exceeded");
              }
}
              });
              }
               
        });
});

我想首先在获取请求的MongoDB中保存一个对象,然后在ejs中使用一个新对象呈现一个页面。但是我收到一个错误500(内部服务器错误),所以我们可以保存一个对象并渲染ejs在同一个终点处有新对象的页面? P.S-下面的代码是代码的关键部分,没有语法错误,schooluser(object)从代码的上半部分传递。

dashboardRouter.route('/:Teachersubjects/:Class/:Salary')
  .get(function(req, res) { 
    vacancies.create(req.params, function(err, vacancy) {
      if (err) {
        console.log(err.message);
        console.log(err.name);
        console.log("create error");
        for (field in err.errors) {
          console.log(err.errors[field].message); 
        }
        handleErr(err);
        return;
      }

      schooluser.vacancies.push(vacancy._id);
      schooluser.save(function(err) {
        if (err) {
          handleErr(err);
        } else {
          res.render('../views/dashboard/dashboard_resumes.ejs', {
            sdata: data
          });
        }
      });        
    });
  });

1 个答案:

答案 0 :(得分:0)

您的路线处理程序存在多个问题。首先,每当发生错误时,您都可以通过handleErr()函数执行此操作,但在这些情况下,您永远不会对res执行任何操作。这意味着请求永远不会被解决,并最终在客户端超时 发生错误时,您应该回复错误代码。例如:

res.status(400).send('Something went wrong');

当您确实发送回复并呈现模板时,您正在应用名为data的变量。但是,此变量似乎不会在代码中的任何位置声明。也许你遗漏了那段代码,但是如果该变量确实是未声明的,它会抛出一个ReferenceError,它被路由框架捕获,而路由框架又以500 Internal Server Error响应。所以这可能是你问题的根源 <{1}}或vacancies.create()引发的任何未捕获的错误都会产生相同的效果。