节点/快速,发送后无法设置标头

时间:2015-05-04 17:55:46

标签: node.js express mongoose

我有两条路线,'/ all'和'question / all',据我所知,应该做同样的事情。问题是一方工作而另一方则没有。

索引路由(/ all)有效,大部分都为我生成了Express,但我从头开始编写了问题路径。

/ question中的所有路由除/ question / all外都有效。我有什么可能忘记的?

这是我得到的错误:

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
    at ServerResponse.header (/Users/rick/projects/express-test/node_modules/express/lib/response.js:700:10)
    at ServerResponse.res.contentType.res.type (/Users/rick/projects/express-test/node_modules/express/lib/response.js:537:15)
    at ServerResponse.send (/Users/rick/projects/express-test/node_modules/express/lib/response.js:129:14)
    at fn (/Users/rick/projects/express-test/node_modules/express/lib/response.js:934:10)
    at View.exports.renderFile [as engine] (/Users/rick/projects/express-test/node_modules/ejs/lib/ejs.js:353:10)
    at View.render (/Users/rick/projects/express-test/node_modules/express/lib/view.js:93:8)
    at EventEmitter.app.render (/Users/rick/projects/express-test/node_modules/express/lib/application.js:566:10)
    at ServerResponse.res.render (/Users/rick/projects/express-test/node_modules/express/lib/response.js:938:7)
    at module.exports (/Users/rick/projects/express-test/app.js:70:7)

这是我的问题 - 路线:

var express = require('express');
var router = express.Router();
var Question = require('../models/question');

// Route: '/questions'
router.post('/new', function(req, res, next) {
    var newQuestion = new Question();
    newQuestion.text = req.body.question;

    newQuestion.save(function (err) {
        if (err)
            res.send(err);
        res.json({
            message: 'new question was created',
            question: newQuestion
         });
    });
});

router.get('/:question_id',function(req, res) {
    Question.findById(req.params.question_id, function(err, question) {
        if (err)
            res.send(err);
        res.json(question);
    });
});

router.get('/all',function(req, res) {
    Question.find(function(err, allQuestions) {
        if (err)
            res.send(err);
        res.json(allQuestions);
    });
});

module.exports = router;

这是我的索引路线:

var express = require('express');
var router = express.Router();
var Question = require('../models/question');

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

router.get('/all',function(req, res) {
    Question.find(function(err, allQuestions) {
        if (err)
            res.send(err);
        res.json(allQuestions);
    });
});

module.exports = router;

0 个答案:

没有答案