Heroku-22栈升级后node-input-validator报TypeError错误求助
问题:Heroku-22栈部署后node-input-validator抛出TypeError: value.toString is not a function
问题描述
POST请求端点出现依赖node-input-validator的TypeError: value.toString is not a function错误。代码在本地环境及其他Heroku账号部署时正常,升级到Heroku-22栈后触发该问题。
错误日志
2022-07-17T07:10:37.706093+00:00 app[web.1]: OPTIONS /v1/auditions/62b4def93aafc20074636f4f/enlist/entries 204 0.146 ms - 0 2022-07-17T07:10:38.127600+00:00 app[web.1]: (node:114) UnhandledPromiseRejectionWarning: TypeError: value.toString is not a function 2022-07-17T07:10:38.127608+00:00 app[web.1]: at Object.reallyEmptyTrimmed (/app/node_modules/node-input-validator/lib/util/empty.js:14:17) 2022-07-17T07:10:38.127608+00:00 app[web.1]: at Object.required (/app/node_modules/node-input-validator/lib/rules/required.js:4:13) 2022-07-17T07:10:38.127609+00:00 app[web.1]: at Validator.apply (/app/node_modules/node-input-validator/lib/validator.js:356:33) 2022-07-17T07:10:38.127609+00:00 app[web.1]: at /app/node_modules/node-input-validator/lib/validator.js:209:29 2022-07-17T07:10:38.127610+00:00 app[web.1]: at Array.forEach (<anonymous>) 2022-07-17T07:10:38.127610+00:00 app[web.1]: at Validator.check (/app/node_modules/node-input-validator/lib/validator.js:203:16) 2022-07-17T07:10:38.127611+00:00 app[web.1]: at createAuditionEntryValidation (/app/dist/server/middlewares/verifyAudition.js:206:5) 2022-07-17T07:10:38.127611+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 2022-07-17T07:10:38.127611+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:144:13) 2022-07-17T07:10:38.127612+00:00 app[web.1]: at /app/dist/server/middlewares/verifySignUp.js:319:5 2022-07-17T07:10:38.127612+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5) 2022-07-17T07:10:38.127893+00:00 app[web.1]: (node:114) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) 2022-07-17T07:10:38.127895+00:00 app[web.1]: (node:114) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
相关中间件代码
const emailAgreementValidation = (req, res, next) => { let file; if (req.files) file = req.files.file; const v = new Validator(req.body, { email: 'required|email', agreement: 'required|boolean', }); v.check().then((matched) => { if (!matched) { if (file) if (file.tempFilePath) deleteFile(file.tempFilePath); res.status(422).send(v.errors); return; } next(); }); };
问题原因及解决方案
核心原因
错误触发在node-input-validator的required规则校验中,调用value.toString()时失败,说明待校验的字段值不是字符串、数字、布尔等具备toString方法的类型,大概率是null、undefined或异常对象。结合环境差异,问题出在Heroku-22栈的以下变化:
- Node.js版本差异:Heroku-22默认Node.js版本更高,导致请求体解析(如express的body-parser)行为变化,部分字段未被正确解析为预期类型。
- 依赖版本不一致:Heroku部署时可能自动升级了
node-input-validator,旧版本的校验逻辑对非字符串类型的兼容性较差。 - 文件上传中间件行为变化:代码中使用
req.files处理文件上传,Heroku-22上的multer等中间件配置或行为改变,导致req.body字段解析异常。
解决方案
锁定依赖版本
在package.json中明确指定node-input-validator的版本,确保本地与Heroku环境依赖一致:"dependencies": { "node-input-validator": "x.x.x" // 替换为本地正常运行的版本号 }显式转换字段类型
在校验前强制转换req.body中字段的类型,避免类型异常:const emailAgreementValidation = (req, res, next) => { let file; if (req.files) file = req.files.file; // 显式转换字段类型 req.body.email = req.body.email ? String(req.body.email) : ''; req.body.agreement = Boolean(req.body.agreement); const v = new Validator(req.body, { email: 'required|email', agreement: 'required|boolean', }); v.check().then((matched) => { if (!matched) { if (file?.tempFilePath) deleteFile(file.tempFilePath); res.status(422).send(v.errors); return; } next(); }).catch(err => { // 捕获验证过程中的异常,避免未处理Promise拒绝 if (file?.tempFilePath) deleteFile(file.tempFilePath); res.status(500).send({ error: '验证失败' }); }); };统一Node.js版本
在package.json中指定Node.js版本,确保Heroku使用与本地一致的版本:"engines": { "node": "14.x" // 替换为本地使用的版本 }检查文件上传中间件配置
确认multer等文件上传中间件的配置在Heroku-22上正常,确保req.body能正确解析请求参数。
内容的提问来源于stack exchange,提问作者sommy
相关产品推荐
相关产品推荐

