简体   繁体   English

节点/ JavaScript重定向不起作用?

[英]Node/Javascript Redirect Not Working?

I have built a form to add some dummy data to a Mongo collection for testing purposes. 我构建了一个表单,用于将一些虚拟数据添加到Mongo集合中以进行测试。 The function to add the data seems to be working ok in that the data is being added ok, but, for some reason, the redirect command at the end of the function is not doing its thing. 添加数据的功能似乎可以正常运行,因为可以正确添加数据,但是由于某种原因,该功能末尾的redirect命令无法正常工作。 The data comes from a bog standard HTML form. 数据来自标准HTML表格格式。 Here is the code for adding the data: 这是添加数据的代码:

router.post('/addacc',function(req,res){
    var sljEdate = "";
    var pljEdate = "";
    var MongoClient = mongodb.MongoClient;
    var url = 'mongodb://localhost:27017/stc';
    MongoClient.connect(url,function(err,db){
        if(err){
            console.log("Connection Error");
        }else{
            console.log("connected to mongo");
            var collection = db.collection('account');
            var query = {accountID: req.body.acc_id,companyName: req.body.acc_name,companyEmail:req.body.acc_email,companyPhone:req.body.acc_phone,companyPostCode:req.body.acc_postcode,
                        photographers: {phtID:"",phtName:"",phtPhone:"",phtEmail:"",phtLoc:"",preferredContact:"",lastJob:"",pljEpoch:pljEdate,phtNotes:""},
                        schools: {schoolID:"",schoolName:"",schoolPhone:"",schoolEmail:"",schoolPostCode:"",lastJob:"",sljEpoch:sljEdate,notes:""}};
            collection.insert(query,function(err,result){
                if(err){
                    console.log("Error inserting Account record",err);
                }else{
                    console.log("Data Insert Success");
                    res.redirect("/forms");
                }
                db.close();
            });
        }
    });
});

I added lines to log to the console in a few places so that I could check progress and they all fire, including the final 'Data Insert Success' one. 我在几处添加了几行记录到控制台,以便我可以检查进度,并且都可以触发,包括最后的“数据插入成功”。 The res.redirect which follows it, however, does nothing. 但是,跟随它的res.redirect不执行任何操作。 In the browser the URL remains as '/addacc', though the node console shows a subsequent GET for the '/forms' URL. 在浏览器中,URL保持为“ / addacc”,尽管节点控制台显示了“ / forms” URL的后续GET。

I've tried a few variations for the redirect, using eg res.send("test") just to try and output something, also a res.render function, but none of them seem to work. 我已经尝试过一些重定向的变体,例如使用res.send(“ test”)只是为了尝试输出一些东西,还有一个res.render函数,但是它们似乎都不起作用。

I'm pretty sure that I have missed something simple, but I've been staring at the code for too long and I can't see anything wrong with it. 我很确定我错过了一些简单的东西,但是我盯着代码看了太久了,我看不到任何错误。 In fact, I've had nearly identical code working lots of times. 实际上,我有几乎相同的代码可以工作很多次。 The only thing I'm doing differently this time is that I'm inserting into a Mongo collection which includes embedded documents, but the data insert is not the problem as the data gets in fine. 这次我要做的唯一不同的事情是,我将插入包含嵌入式文档的Mongo集合中,但是由于数据很好,因此数据插入不是问题。

Help please! 请帮助!

Ok, it seems that the Node problem above was a bit of a red herring. 好的,上面的Node问题似乎有点像鲱鱼。 The redirect is actually working ok (MrWillihog's comment above is duly noted for future reference anyway). 重定向实际上是可以的(无论如何,上面提到的MrWillihog的评论已被适当注明,以供将来参考)。 I thought it must be as all the response codes on the npm console checked out fine. 我认为一定是因为npm控制台上的所有响应代码都已签出。 The problem is actually to do with the rendering of the Jade template. 问题实际上与Jade模板的呈现有关。 Specifically something to do with the the file layout.jade which I use to set out the HTML head. 特别是与用于设置HTML头的文件layout.jade有关。 For some reason (yet to be determined) things work fine when calling the /forms page directly in the URL. 出于某些原因(尚未确定),直接在URL中调用/ forms页面时,一切正常。 The file forms.jade starts in the standard way with 文件form.jade以标准方式以

extends layout

block content

With the opening BODY tag in layout.jade the page fails to load properly when called from the redirect: the layout.jade file renders (I put an H1 in there to check), but forms.jade doesn't. 在layout.jade中使用打开的BODY标记时,从重定向调用时页面无法正确加载:layout.jade文件呈现(我在其中放置了H1进行检查),而Forms.jade没有。

I moved the BODY down in to forms.jade (under block.content) and it started working though, interestingly, the URL in the address bar remains as '/accadd' 我将BODY移至form.jade(在block.content下),它开始工作,但是有趣的是,地址栏中的URL仍为'/ accadd'

Again I'm a bit stumped as I have used this pattern quite a few times with no issue. 再次让我有些困惑,因为我已经多次使用此模式,没有任何问题。 I'm guessing that I've just forgotten something simple. 我猜我只是忘记了一些简单的事情。 I'll keep digging until I find the answer, but at least now I should be looking in the right place. 我会一直在挖掘,直到找到答案为止,但至少现在我应该在正确的位置进行寻找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM