无法完成GET请求

时间:2016-07-28 16:33:12

标签: javascript node.js mongodb mongoose

我正在尝试创建一个表单来收集电子邮件地址。电子邮件地址POST到/ dreamjob。

当我尝试访问/电子邮件列表时,没有出现任何错误,但页面未加载&超时我无法弄清楚这个问题。我如何更改代码以更正此问题?谢谢!

router.post("/dreamjob", function(req, res){

    //Create Email
    Email.create(req.body.email, function(err, newEmail){
        if(err){
            res.render("station.ejs");
        } else {
            newEmail.save();
            console.log(req.body.email)
        }
    });
});


//All Emails: 
    router.get("/emails", function(req, res){
    Email.find, function(err, email){
        if (err){
        console.log("ERROR!");
         } else { 
    res.render("emails", {Email: email});
    }
    }
    });

型号:

var mongoose = require("mongoose");

var emailSchema = new mongoose.Schema({
   email: String
});

module.exports = mongoose.model("Email", emailSchema);

1 个答案:

答案 0 :(得分:1)

当您创建电子邮件时,如果出现错误,您只回复该请求,请尝试以下操作:

 Email.create(req.body.email, function(err, newEmail){
    if(err){
        res.render("station.ejs");
    } else {
        newEmail.save();
        console.log(req.body.email);
        res.send(200); // reply to the http request here
    }
});