res.query 错误中的默认值
default values in res.query error
我想为 res.query 参数设置默认值,这意味着 api 不需要来自客户端的参数。因此我想回退到默认值。这是我回复请求的方法:
exports.all = (req, res, next) => {
const filterParam = req.query.filterBy;
const loc = req.query.loc;
let whereCondition = {isPublic: true};
let findCondition = {};
let pageOptions = {
skip: req.query.skip || 0,
limit: req.query.limit || 10
};
if (filterParam == 'building') {
whereCondition.type = 'building'
} else if (filterParam == 'center') {
whereCondition.type = 'center';
} else if (filterParam == 'geo') {
const distanceInKm = 125 / 111.2;
findCondition.loc = { $near: [loc[1], loc[0]], $maxDistance: distanceInKm }
}
building
.find(findCondition)
.skip(pageOptions.skip)
.limit(pageOptions.limit)
.where(whereCondition)
.exec((err, buildings) => {
if (err) { res.status(400).json({error: err.data}); }
return res.status(200).json({buildings: buildings});
});
};
错误
GET /api/building/all?limit=4&skip=4 200 13.758 ms - 2
events.js:160
throw er; // Unhandled 'error' event
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/path/to/project/server/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/path/to/project/server/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/path/to/project/server/node_modules/express/lib/response.js:249:15)
at Building.find.skip.limit.where.exec (/path/to/project/server/controllers/BuildingCtrl.js:197:30)
at /path/to/project/server/node_modules/mongoose/lib/query.js:2260:9
at process._tickCallback (internal/process/next_tick.js:103:7)
您需要在if
条件中添加return
语句。我认为 mongoose 查询有问题,它 returns 一个错误:
...
.exec((err, buildings) => {
if (err) {
return res.status(400).json({error: err.data});
}
res.send({buildings: buildings});
});
顺便说一句,express 默认设置 200 个状态,send
方法根据响应数据设置 content-type
。
我想为 res.query 参数设置默认值,这意味着 api 不需要来自客户端的参数。因此我想回退到默认值。这是我回复请求的方法:
exports.all = (req, res, next) => {
const filterParam = req.query.filterBy;
const loc = req.query.loc;
let whereCondition = {isPublic: true};
let findCondition = {};
let pageOptions = {
skip: req.query.skip || 0,
limit: req.query.limit || 10
};
if (filterParam == 'building') {
whereCondition.type = 'building'
} else if (filterParam == 'center') {
whereCondition.type = 'center';
} else if (filterParam == 'geo') {
const distanceInKm = 125 / 111.2;
findCondition.loc = { $near: [loc[1], loc[0]], $maxDistance: distanceInKm }
}
building
.find(findCondition)
.skip(pageOptions.skip)
.limit(pageOptions.limit)
.where(whereCondition)
.exec((err, buildings) => {
if (err) { res.status(400).json({error: err.data}); }
return res.status(200).json({buildings: buildings});
});
};
错误
GET /api/building/all?limit=4&skip=4 200 13.758 ms - 2
events.js:160
throw er; // Unhandled 'error' event
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/path/to/project/server/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/path/to/project/server/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/path/to/project/server/node_modules/express/lib/response.js:249:15)
at Building.find.skip.limit.where.exec (/path/to/project/server/controllers/BuildingCtrl.js:197:30)
at /path/to/project/server/node_modules/mongoose/lib/query.js:2260:9
at process._tickCallback (internal/process/next_tick.js:103:7)
您需要在if
条件中添加return
语句。我认为 mongoose 查询有问题,它 returns 一个错误:
...
.exec((err, buildings) => {
if (err) {
return res.status(400).json({error: err.data});
}
res.send({buildings: buildings});
});
顺便说一句,express 默认设置 200 个状态,send
方法根据响应数据设置 content-type
。