NodeJs发送后无法设置标头

时间:2015-09-23 08:35:11

标签: node.js error-handling header response

在我的项目中,nodejs返回以下消息:

错误:发送后无法设置标头。 我在这个网站上看了很多问题,但我仍然无法找到解决方案。请帮忙。!!!

这是我的代码:

routes.js

var mongodb = require(__dirname+"/db.js");
var fs = require("fs");
var hbs = require("express3-handlebars");
var soap = require('soap');
var url = "http://infovalutar.ro/curs.asmx?wsdl";
var index = null;
module.exports = {
    home: function(req, res) {
        mongodb.db().collection("categories").find({id: req.params.category}).toArray(function(err, data) {

            for (j=0; j<data.length; j++) {
                for(i=0; i<data[j].categories.length; i++) {
                    var link_path = data[j].categories[i].categories;
                    link_path.type = { data: req.params.categorie };
                    res.render("home", {
                        location: data[j].id,
                        title: data[j].page_title,
                        name: data[j].name,
                        description: data[j].page_description,
                        mens_link: "/mens",
                        womens_link: "/womens",
                        data_id: data[j].id,
                        type: req.params.categoriy,
                        cat: data[j].categories,
                        categories: data[j].categories[i].categories
                    });
                }
            }   
        });
    },
    product_list: function(req, res) {
        mongodb.db().collection("products").find({primary_category_id: req.params.sub_category}).toArray(function(err, data) {

            for (s=0; s<data.length; s++) {
                for (v=0; v<data[s].image_groups.length; v++) {

                    res.render("product_list", {
                        products: data,
                        mens_link: "/mens",
                        womens_link: "/womens",
                        main: req.params.category,
                        sub: req.params.sub_category,
                        id: req.params.id,

                    });
                }
            }
        });
    },
    single_product: function(req, res) {
        mongodb.db().collection("products").find({id: req.params.id}).toArray(function(err, data) {
        for (s=0; s<data.length; s++) {
            for (v=0; v<data[s].image_groups.length; v++) {

                res.render("product", {
                    single_product: data,
                    mens_link: "/mens",
                    womens_link: "/womens",
                    main: req.params.category,
                    sub: req.params.sub_category,
                    id: req.params.id,
                });
            }
        }

        });
    }
};

1 个答案:

答案 0 :(得分:1)

不幸的是,这种方式不起作用。您正在调用函数render(),使渲染引擎发送指定的布局数据并关闭连接。这正是您收到标题已发送错误的原因。

每个请求只应调用一次函数res.render()

创建一个数组var response=[]并像这样更改循环:

        var response=[];
        for (j=0; j<data.length; j++) {
            for(i=0; i<data[j].categories.length; i++) {
                var link_path = data[j].categories[i].categories;
                link_path.type = { data: req.params.categorie };
                response.push({
                    location: data[j].id,
                    title: data[j].page_title,
                    name: data[j].name,
                    description: data[j].page_description,
                    mens_link: "/mens",
                    womens_link: "/womens",
                    data_id: data[j].id,
                    type: req.params.categoriy,
                    cat: data[j].categories,
                    categories: data[j].categories[i].categories
                });
            }
        }   
        res.render('home', response);

并在for循环中,将每个待渲染项目推送到该数组中。

请注意,您将在home布局中的响应中获得一系列对象。