Express.js - 无法使用导出函数设置 Headers
Express.js - Cannot Set Headers with exported function
学习如何使用 Mocha、Chai、Chai-HTTP[= 使用 Express 进行测试40=] 插件,以及 MongoDB 和 Mongoose。我有一个测试,目的是检测 MongoDB 是否会因尝试使用错误的 _id
值(太短)查找文档而发回错误。
我注意到我的部分代码在我的其他 Express 路由中重复,并且想将它重新用于其他路由,所以我从另一个模块导出了它,但现在我明白了:
Uncaught Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
不确定为什么会出现此错误。如果我有相同的代码,作为导出的函数,在路由代码中它工作正常,但导出它只是抱怨。
代码如下:
test/route/example.test.js 片段
it('Bad request with a too short ID string (12 characters minimum)', () => {
// /api/v1/example is the endpoint
// /blah is the param
chai.request(app).get('/api/v1/example/blah').end((err, res) => {
// Insert all the nice assert stuff. :)
});
});
route/example.js 片段
// Packages
const router = require('express').Router();
// Models (Mongoose Schemas)
const Example = require('../models/example.model');
// Helpers
const { foundMongoError } = require('../helpers/routes');
// -----Snipped-----
router.route('/:exampleId').get((req, res) => {
// Retrieve the exampleId parameter.
const exampleId = req.params.exampleId;
Example.findById(exampleId, (mongoError, mongoResponse) => {
foundMongoError(mongoError, res); // Having an issue
// If I have the same code that makes up foundMongoError inside here, no issues,
// but it will no longer be DRY.
// Check if any responses from MongoDB
if(mongoResponse) {
res.status(200).json(mongoResponse);
} else {
return res.status(404).json({
errorCode: 404,
errorCodeMessage: 'Not Found',
errorMessage: `Unable to find example with id: ${exampleId}.`
});
}
});
});
helpers/routes.js
const foundMongoError = (mongoError, res) => {
if(mongoError) {
return res.status(400).json({
errorCode: 400,
errorCodeMessage: 'Bad Request',
errorMessage: mongoError.message
});
}
};
module.exports = {
foundMongoError
};
这意味着您发送并响应 res
两次。第一次发回这里:
if(mongoError) {
return res.status(400).json({
errorCode: 400,
errorCodeMessage: 'Bad Request',
errorMessage: mongoError.message
});
}
你发回了一个响应,但函数仍在继续工作,这意味着函数一直运行到这里:
if(mongoResponse) {
res.status(200).json(mongoResponse);
} else {
return res.status(404).json({
errorCode: 404,
errorCodeMessage: 'Not Found',
errorMessage: `Unable to find example with id: ${exampleId}.`
});
}
此处出现第二个响应,此处出现错误。
我会这样重写代码:
而不是 return 响应,你 return 一个 true
这意味着有错误,否则 false
:
const foundMongoError = (mongoError, res) => {
if(mongoError) {
res.status(400).json({
errorCode: 400,
errorCodeMessage: 'Bad Request',
errorMessage: mongoError.message
});
return true;
}
return false;
};
module.exports = {
foundMongoError
};
那你可以这样写:
if(foundMongoError(mongoError, res)) return;
return
会停止函数执行剩下的代码
学习如何使用 Mocha、Chai、Chai-HTTP[= 使用 Express 进行测试40=] 插件,以及 MongoDB 和 Mongoose。我有一个测试,目的是检测 MongoDB 是否会因尝试使用错误的 _id
值(太短)查找文档而发回错误。
我注意到我的部分代码在我的其他 Express 路由中重复,并且想将它重新用于其他路由,所以我从另一个模块导出了它,但现在我明白了:
Uncaught Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
不确定为什么会出现此错误。如果我有相同的代码,作为导出的函数,在路由代码中它工作正常,但导出它只是抱怨。
代码如下:
test/route/example.test.js 片段
it('Bad request with a too short ID string (12 characters minimum)', () => {
// /api/v1/example is the endpoint
// /blah is the param
chai.request(app).get('/api/v1/example/blah').end((err, res) => {
// Insert all the nice assert stuff. :)
});
});
route/example.js 片段
// Packages
const router = require('express').Router();
// Models (Mongoose Schemas)
const Example = require('../models/example.model');
// Helpers
const { foundMongoError } = require('../helpers/routes');
// -----Snipped-----
router.route('/:exampleId').get((req, res) => {
// Retrieve the exampleId parameter.
const exampleId = req.params.exampleId;
Example.findById(exampleId, (mongoError, mongoResponse) => {
foundMongoError(mongoError, res); // Having an issue
// If I have the same code that makes up foundMongoError inside here, no issues,
// but it will no longer be DRY.
// Check if any responses from MongoDB
if(mongoResponse) {
res.status(200).json(mongoResponse);
} else {
return res.status(404).json({
errorCode: 404,
errorCodeMessage: 'Not Found',
errorMessage: `Unable to find example with id: ${exampleId}.`
});
}
});
});
helpers/routes.js
const foundMongoError = (mongoError, res) => {
if(mongoError) {
return res.status(400).json({
errorCode: 400,
errorCodeMessage: 'Bad Request',
errorMessage: mongoError.message
});
}
};
module.exports = {
foundMongoError
};
这意味着您发送并响应 res
两次。第一次发回这里:
if(mongoError) {
return res.status(400).json({
errorCode: 400,
errorCodeMessage: 'Bad Request',
errorMessage: mongoError.message
});
}
你发回了一个响应,但函数仍在继续工作,这意味着函数一直运行到这里:
if(mongoResponse) {
res.status(200).json(mongoResponse);
} else {
return res.status(404).json({
errorCode: 404,
errorCodeMessage: 'Not Found',
errorMessage: `Unable to find example with id: ${exampleId}.`
});
}
此处出现第二个响应,此处出现错误。
我会这样重写代码:
而不是 return 响应,你 return 一个 true
这意味着有错误,否则 false
:
const foundMongoError = (mongoError, res) => {
if(mongoError) {
res.status(400).json({
errorCode: 400,
errorCodeMessage: 'Bad Request',
errorMessage: mongoError.message
});
return true;
}
return false;
};
module.exports = {
foundMongoError
};
那你可以这样写:
if(foundMongoError(mongoError, res)) return;
return
会停止函数执行剩下的代码