我正在编写一个简单的查询来获取mongodb中的所有课程列表。我得到一个{}作为响应,node.js因错误而停止,“错误:发送后无法设置标题。”
这是我的课程集合架构。 course.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var course = new Schema({
course : [{courseName : String}]
}, {collection : 'course'});
module.exports = mongoose.model('course', course);
这是查询的index.js文件中的router.get。
router.get('/course', function (req, res) {
collection = db.collection('course');
query = {};
projection = {"_id" : 0, "course.courseName": 1};
cursor = collection.find(query);
cursor.project(projection);
cursor.forEach(function (doc) {
res.send(doc);
},
function (err) {
if(err)
throw err;
}
);
});
这是我得到的错误。
GET /course 200 34.808 ms - 2
/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:175
throw err;
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/express/lib/response.js:249:15)
at ServerResponse.send (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/express/lib/response.js:151:21)
at /home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/routes/index.js:234:19
at /home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/lib/cursor.js:773:23
at handleCallback (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:172:5)
at nextFunction (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:682:5)
at Cursor.next [as _next] (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:692:3)
at loop (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/lib/cursor.js:687:8)
at _each (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/lib/cursor.js:734:16)
at /home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/lib/cursor.js:745:7
at handleCallback (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/lib/utils.js:96:12)
at /home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/lib/cursor.js:675:5
at handleCallback (/home/vikas/study/angularjs/learnAngularjsCoursera/passportAuthentication/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:172:5)
请建议我哪里出错了。
答案 0 :(得分:3)
使用快递,您每条路线只能拨打一次res.send
。在您的情况下,您似乎正在尝试将文档列表返回给客户端,对吗?
您需要做的是先创建一个对象数组,然后将其返回。
以下是如何修改现有路线以便正常工作的方法:
router.get('/course', function(req, res) {
var collection = db.collection('course');
var query = {};
var projection = {"_id" : 0, "course.courseName": 1};
var docs = [];
cursor = collection.find(query);
cursor.project(projection);
cursor.forEach(function(doc) {
docs.push(doc);
}, function (err) {
if (err) throw err;
res.json({docs: docs});
});
});
答案 1 :(得分:0)
可以使用回调函数和return res.send
尝试所有数据。
像:
router.get('/course', function (req, res) {
var collection = db.collection('course');
var query = {};
var projection = {"_id" : 0, "course.courseName": 1};
collection.find(query, projection).toArray(function (err, docs) {
if(err) {
return res.status(500).send({error:err.message});
}
return res.status(200).send(docs);
});
});
你可以根据你的问题描述使用猫鼬。所以你应该使用它而不是collection = db.collection('course');
。首先加载course
模型,然后使用
var Course = require("course");// your course.js file path. can be like: "../model/course"
Course.find(query, projection, function (err, docs){
//... like above function
});