在我的代码中,我必须从请求参数对象中拆分键和值,以通过 ObjectID
验证该值。因此,我在条目数组上使用了映射,并解构了键和值,以便我可以测试该值。但我收到错误:
'_key' is defined but never used
我该如何解决这个问题?
export const validateObjectId = (request: Request, response: Response, next: NextFunction) => {
Object.entries(request.params).map(([_key, param]) => {
if (!ObjectId.isValid(param)) {
return errorConstants.sendErrorResponse(response, errorConstants.invalid_object_id);
}
});
next();
};
最佳答案
既然您不使用键,只使用值,为什么不直接使用 Object.values
而不是 Object.entries
?
另请注意,由于您没有使用结果数组,因此您可能应该使用 .forEach
而不是 .map
,并且不使用返回值,所以不需要返回
:
export const validateObjectId = (request: Request, response: Response, next: NextFunction) => {
Object.values(request.params).forEach((param) => {
if (!ObjectId.isValid(param)) {
errorConstants.sendErrorResponse(response, errorConstants.invalid_object_id);
}
});
next();
};
关于javascript - 如何声明函数中未使用的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260783/