运行 npm 测试时得到的错误"Can't set headers after they are sent"



我在控制台上收到以下错误: 这是我得到的完整信息;

server running on port 5000
API Orders Endpoint Testing
1) should list ALL orders on / GET
2) should get a SINGLE order on /:id GET
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsf                                ast-food-fastnode_modulesexpresslibresponse.js:767:10)
at ServerResponse.send (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfas                                t-food-fastnode_modulesexpresslibresponse.js:170:12)
at ServerResponse.json (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfas                                t-food-fastnode_modulesexpresslibresponse.js:267:15)
at ServerResponse.send (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfas                                t-food-fastnode_modulesexpresslibresponse.js:158:21)
at getOrderById (C:/Users/Alhaja-Adams-R-K/documents/web-projects/fast-food-                                
fast/controllers/orders.js:31:26)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-foo                                d-fastnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnode_modu                                lesexpresslibrouterindex.js:281:22
at param (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastno                                de_modulesexpresslibrouterindex.js:354:14)
at param (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastno                                de_modulesexpresslibrouterindex.js:365:14)
at Function.process_params (C:UsersAlhaja-Adams-R-Kdocumentsweb-projects                                fast-food-fastnode_modulesexpresslibrouterindex.js:410:3)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterindex.js:275:10)
at Function.handle (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-fo                                od-fastnode_modulesexpresslibrouterindex.js:174:3)
at router (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastn                                ode_modulesexpresslibrouterindex.js:47:12)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-f                                astnode_modulesexpresslibrouterindex.js:317:13)
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnode_modu                                lesexpresslibrouterindex.js:284:7
at Function.process_params (C:UsersAlhaja-Adams-R-Kdocumentsweb-projects                                fast-food-fastnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterindex.js:275:10)
at jsonParser (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fa                                stnode_modulesexpressnode_modulesbody-parserlibtypesjson.js:109:7)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-f                                astnode_modulesexpresslibrouterindex.js:317:13)
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnode_modu                                lesexpresslibrouterindex.js:284:7
at Function.process_params (C:UsersAlhaja-Adams-R-Kdocumentsweb-projects                                fast-food-fastnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterindex.js:275:10)
at urlencodedParser (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-f                                ood-fastnode_modulesbody-parserlibtypesurlencoded.js:91:7)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-f                                astnode_modulesexpresslibrouterindex.js:317:13)
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnode_modu                                lesexpresslibrouterindex.js:284:7
at Function.process_params (C:UsersAlhaja-Adams-R-Kdocumentsweb-projects                                fast-food-fastnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterindex.js:275:10)
at jsonParser (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fa                                stnode_modulesbody-parserlibtypesjson.js:110:7)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-f                                astnode_modulesexpresslibrouterindex.js:317:13)
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnode_modu                                lesexpresslibrouterindex.js:284:7
at Function.process_params (C:UsersAlhaja-Adams-R-Kdocumentsweb-projects                                fast-food-fastnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterindex.js:275:10)
at expressInit (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-f                                astnode_modulesexpresslibmiddlewareinit.js:40:5)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-f                                astnode_modulesexpresslibrouterindex.js:317:13)
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnode_modu                                lesexpresslibrouterindex.js:284:7
at Function.process_params (C:UsersAlhaja-Adams-R-Kdocumentsweb-projects                                fast-food-fastnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterindex.js:275:10)
at query (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastno                                de_modulesexpresslibmiddlewarequery.js:45:5)
at Layer.handle [as handle_request] (C:UsersAlhaja-Adams-R-Kdocumentsweb                                -projectsfast-food-fastnode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-f                                astnode_modulesexpresslibrouterindex.js:317:13)
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnode_modu                                lesexpresslibrouterindex.js:284:7
at Function.process_params (C:UsersAlhaja-Adams-R-Kdocumentsweb-projects                                fast-food-fastnode_modulesexpresslibrouterindex.js:335:12)
at next (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fastnod                                e_modulesexpresslibrouterindex.js:275:10)
at Function.handle (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-fo                                od-fastnode_modulesexpresslibrouterindex.js:174:3)
at Function.handle (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-fo                                od-fastnode_modulesexpresslibapplication.js:174:10)
at Server.app (C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fa                                stnode_modulesexpresslibexpress.js:39:9)
at emitTwo (events.js:126:13)
at Server.emit (events.js:214:7)
at parserOnIncoming (_http_server.js:619:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:115:23)
3) should get place an order on / POST
4) should update the status of an order on /:id GET
0 passing (344ms)
4 failing
1) API Orders Endpoint Testing
should list ALL orders on / GET:
Uncaught AssertionError: expected { Object (success, message, ...) } to have property 'name'
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fasttesttest.js:26:25
at Test.Request.callback (node_modulessuperagentlibnodeindex.js:716:12)
at parser (node_modulessuperagentlibnodeindex.js:916:18)
at IncomingMessage.res.on (node_modulessuperagentlibnodeparsersjson.js:19:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
2) API Orders Endpoint Testing
should get a SINGLE order on /:id GET:
Uncaught AssertionError: expected { Object (success, message, ...) } to have property 'name'
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fasttesttest.js:37:25
at Test.Request.callback (node_modulessuperagentlibnodeindex.js:716:12)
at parser (node_modulessuperagentlibnodeindex.js:916:18)
at IncomingMessage.res.on (node_modulessuperagentlibnodeparsersjson.js:19:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
3) API Orders Endpoint Testing
should get place an order on / POST:
Uncaught AssertionError: expected { Object (success, message, ...) } to have property 'name'
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fasttesttest.js:53:25
at Test.Request.callback (node_modulessuperagentlibnodeindex.js:716:12)
at parser (node_modulessuperagentlibnodeindex.js:916:18)
at IncomingMessage.res.on (node_modulessuperagentlibnodeparsersjson.js:19:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
4) API Orders Endpoint Testing
should update the status of an order on /:id GET:
Uncaught AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but got 400
+ expected - actual
-400
+200
at C:UsersAlhaja-Adams-R-Kdocumentsweb-projectsfast-food-fasttesttest.js:62:20
at Test.Request.callback (node_modulessuperagentlibnodeindex.js:716:12                                )
at parser (node_modulessuperagentlibnodeindex.js:916:18)
at IncomingMessage.res.on (node_modulessuperagentlibnodeparsersjson.js:19:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)

每当我运行 npm 测试并且我对带有摩卡和 chai 的 API 测试没有通过时,我都会收到错误。 我使用 MVC 方法。我已经从应用程序.js文件中导出了我的应用程序/服务器。但是,我仍然收到此错误。 拜托,我做错了什么?

测试/测试.js

import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../app.js';
chai.use(chaiHttp);
let should = chai.should();
describe('API Orders Endpoint Testing', () => {
//Test to get all orders
it('should list ALL orders on / GET', (done) => {
chai.request(app)
.get('/api/v1/orders')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('name');
res.body.should.have.property('quantity');
done();
});
});
//Test to Fetch a single order
it('should get a SINGLE order on /:id GET', (done) => {
const id = 1;
chai.request(app)
.get(`/api/v1/orders/${id}`)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('name');
res.body.should.have.property('quantity');
done();
});
});
//Test to place a new order
it('should get place an order on / POST', (done) => {
chai.request(app)
.post('/api/v1/orders')
.send({
orderStatus: "Delivered",
name: "Fish-Roll",
quantity: "10",
price: "100",
date: new Date().toDateString()
})
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('name');
res.body.should.have.property('quantity');
done();
});
});
//Test to update status of an order
it('should update the status of an order on /:id GET', (done) => {
const id = 2;
chai.request(app)
.put(`/api/v1/orders/${id}`)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('name');
res.body.should.have.property('quantity');
done();
});
});
}); 

控制器/订单.js

import db from '../db/db';
//ALL ORDERS
export const allOrders = (req, res) => {
res.status(200).send({
success: 'true',
message: 'orders retrieved successfully',
orders: db
});
}
//FETCH AN ORDER
export const getOrderById = (req, res) => {
const id = parseInt(req.params.id, 10);
db.map((order) => {
if (order.id === id) {
return res.status(200).send({
success: 'true',
message: 'order retrieved successfully',
order
});
}
});
return res.status(404).send({
success: 'false',
message: 'order does not exist'
});
}
//PLACE AN ORDER
export const placeAnOrder = (req, res) =>{
if(!req.body.name) {
return res.status(400).send({
success: 'false',
message: 'name is required'
});
} else if(!req.body.quantity) {
return res.status(400).send({
success: 'false',
message: 'quantity is required'
});
}
const order = {
id: db.length + 1,
orderStatus: req.body.status,
name: req.body.name,
quantity: req.body.quantity,
price: "10",
date: new Date().toDateString()
}
db.push(order);
return res.status(200).send({
success: 'true',
message: 'order added successfully',
order: order
})
}
//UPDATE ORDER STATUS
export const updateOrderStatus = (req, res) => {
const id = parseInt(req.params.id, 10);
let orderFound;
let itemIndex;
db.map((order, index) => {
if (order.id === id) {
orderFound = order;
itemIndex = index;
}
});
if (!orderFound) {
return res.status(404).send({
success: 'false',
message: 'order not found'
});
}
if (!req.body.name) {
return res.status(400).send({
success: 'false',
message: 'name is required'
});
} else if (!req.body.quantity) {
return res.status(400).send({
success: 'false',
message: 'quantity is required'
});
}
const updatedOrder = {
id: orderFound.id,
name: req.body.name || orderFound.name,
quantity: req.body.quantity || orderFound.quantity
};
db.splice(itemIndex, 1, updatedOrder);
return res.status(200).send({
success: 'true',
message: 'order added successfully',
updatedOrder: updatedOrder
});
}

.map中返回不会返回函数外getOrderById。似乎您的代码正在尝试在映射完成并发送状态200(如果找到(后执行第res.status(404)行。

尝试在此处使用for循环 -

export const getOrderById = (req, res) => {
const id = parseInt(req.params.id, 10);
for(var i = 0; i < db.length; i++){
var order = db[i];
if (order.id === id) {
return res.status(200).send({
success: 'true',
message: 'order retrieved successfully',
order
});
break;
}
}
return res.status(404).send({
success: 'false',
message: 'order does not exist'
});
}

相关内容

最新更新