TypeError:预期的字符串,但收到未定义的字符串

Posted

技术标签:

【中文标题】TypeError:预期的字符串,但收到未定义的字符串【英文标题】:TypeError: Expected string but received a undefined 【发布时间】:2020-01-31 21:45:03 【问题描述】:

在将名称和密码发布到 mongodb 数据库之前验证名称和密码时,它显示了一些错误

const Validator = require('validator');
const isEmpty = require('./is-empty');

module.exports = function validateRegisterInput(data)
    let errors = ;

    data.name = !isEmpty(data.name) ? data.name : '';
    data.email = !isEmpty(data.email) ? data.email : '';
    data.password = !isEmpty(data.password) ? data.password : '';
    data.password = !isEmpty(data.password2) ? data.password2 : '';


    if((!Validator.isLength(data.name ,  min:2, max:20 )))
        errors.name = 'Name must be between 2 and 20 characters';
    

    if(Validator.isEmpty(data.name))
        errors.name ='Name field is required';
    

    if(Validator.isEmpty(data.email))
        errors.email ='Email field is required';
    

    if(Validator.isEmail(data.email))
        errors.email ='Email is invalid';
    

    if(Validator.isEmpty(data.password))
        errors.password ='Password field is required';
    

    if(Validator.isLength(data.password, min:8 ))
        errors.password ='Password field is required';
    

    if(Validator.isEmpty(data.password2))
        errors.password2 ='Confirm Passwords field is required';
    

    if(Validator.equals(data.password, data.password2))
        errors.password2 = 'Passwords must match';
    

    return 
        errors,
        isValid: isEmpty(errors)
    ;
;

邮递员显示错误

TypeError: Expected string but received an undefined.   在 assertString (E:\MERN\devconnector\node_modules\validator\lib\util\assertString.js:28:11)  在 Object.isEmpty (E :\MERN\devconnector\node_modules\validator\lib\isEmpty.js:19:29)   在 validateRegisterInput (E:\MERN\devconnector\validation\register.js:38:18)  在 router.post (E:\MERN\devconnector\routes\api\users.js:27:33)  在 Layer.handle [as handle_request] (E:\MERN\devconnector\node_modules\express\lib\router\layer.js :95:5)   在下一个 (E:\MERN\devconnector\node_modules\express\lib\router\route.js:137:13)  在 Route.dispatch (E:\MERN\devconnector\node_modules \express\lib\router\route.js:112:3)   在 Layer.handle [as handle_request] (E:\MERN\devconnector\node_modules\express\lib\router\layer.js:95:5)    在 E:\MERN\devconnector\node_modules\express\lib\router\index.js:281:22  在 Function.process_params (E:\MERN\devconnector\node_modules\express\lib\router\index .js:335:12)在下一个 (E:\MERN\devconnector\node_modules\express\lib\router\index.js:275:10)   在 Function.handle (E:\MERN\devconnector\node_modules\express\lib\router\index .js:174:3)   在路由器 (E:\MERN\devconnector\node_modules\express\lib\router\index.js:47:12)  在 Layer.handle [as handle_request] (E: \MERN\devconnector\node_modules\express\lib\router\layer.js:95:5)   在 trim_prefix (E:\MERN\devconnector\node_modules\express\lib\router\index.js:317:13)    在 E:\MERN\devconnector\node_modules\express\lib\router\index.js:284:7

【问题讨论】:

【参考方案1】:

请检查您在 isEmpty.js 文件中声明 isEmpty 函数的位置,您可能使用了箭头函数,但不应该。

const isEmpty = value => 
    value === undefined ||
    value === null ||
    (typeof value === "Object" && Object.keys(value).length === 0) ||
    (typeof value === "string" && value.trim().length === 0)


所以,删除箭头函数“”末尾的大括号,它不应该在那里。

【讨论】:

【参考方案2】:

尝试添加这个:

data.password**2** = !isEmpty(data.password2) ? data.password2 : ''; 

【讨论】:

【参考方案3】:

你必须添加

if("HERE"Validator.equals(data.password, data.password2))
        errors.password2 = 'Passwords must match';

并在第 10 行将 "data.password" 更改为 data.password2

【讨论】:

【参考方案4】:

我建议以 JSON 格式写入数据,然后检查应该是 application/json 的内容类型。同样的问题发生在我身上,我按照我提到的方式解决了它。

【讨论】:

再看看How to Answer。这更适合发表评论,因为它更像是一般性建议,而不是对特定问题的完整答案【参考方案5】:

如果你写的是多行箭头函数,那么我们应该有明确的返回值或结果。

  const isEmpty = value => 
  return (value === undefined ||
    value === null ||
    (typeof value === "Object" && Object.keys(value).length === 0) ||
    (typeof value === "string" && value.trim().length === 0))

;

module.exports = isEmpty;

注意:请参阅 ES6 文档以了解清楚:http://es6-features.org/

【讨论】:

【参考方案6】:

看到您使用 Validator 包,我了解您想要实现的目标,因为我刚刚从重复的 TypeErrors 中走出来。我也有一些错字,我必须改正。所以请确保你没有任何东西。

在这种情况下,我会以这种方式编写代码。这有点冗长,我会尽快重构它,我现在才知道,当我看到这个问题时,我想提供我的意见。

const Validator = require("validator");
const isEmpty = require("./is-empty");
// we cant use isEmpty from validator pkg as it needs its param to be a string

// data is req.body
module.exports = function validateRegisterInput(data) 
    let errors = ;

    data.name = isEmpty(data.name) ? "" : data.name;
    data.email = isEmpty(data.email) ? "" : data.email;
    data.password = isEmpty(data.password) ? "" : data.password;
    data.password2 = isEmpty(data.password2) ? "" : data.password2;

    if (!Validator.isLength(data.name,  min: 2, max: 30 )) 
        errors.name = "Name must be between 2 and 30 characters";
    

    if (Validator.isEmpty(data.name)) 
        errors.name = "Name field is required";
    

    if (Validator.isEmpty(data.email)) 
        errors.email = "Email field is required";
    

    // runs if not in email exists but not in format
    if (!Validator.isEmpty(data.email) && !Validator.isEmail(data.email)) 
        errors.email = "Email is invalid";
    

    if (Validator.isEmpty(data.password)) 
        errors.password = "Password field is required";
    

    // runs if password exists but not in proper format
    if (
        !Validator.isEmpty(data.password) &&
        !Validator.isLength(data.password,  min: 6, max: 30 )
    ) 
        errors.password = "Password must be at least 6 chracters";
    

    if (Validator.isEmpty(data.password2)) 
        errors.password2 = "Password 2 is required";
    

    // runs if not in password 2 exists but not in format
    if (
        !Validator.isEmpty(data.password2) &&
        !Validator.equals(data.password, data.password2)
    ) 
        errors.password2 = "Passwords must match";
    

    return  errors, isValid: isEmpty(errors) ;
;

【讨论】:

【参考方案7】:

我遇到了同样的问题,我通过在检查 (isEmail, isLength, ...) 之前检查字段是否不是 (isEmpty) 来修复它

例如:

if (!Validator.isLength(data.name,  min: 2, max: 30 )) 
  errors.name = "Name must be between 2 and 30 characters"

变成:

if (
    !Validator.isEmpty(data.name) &&
    !Validator.isLength(data.name,  min: 2, max: 30 )
) 
    errors.name = "Name must be between 2 and 30 characters";

【讨论】:

【参考方案8】:

类似的事情发生在我身上。我正在验证一个名为“名称”的密钥,但在邮递员上我发送的是“用户名”。

仔细检查您在邮递员中的密钥是否与您的数据完全匹配。随便

【讨论】:

以上是关于TypeError:预期的字符串,但收到未定义的字符串的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:预期的喉咙大小是一个数字,但未定义

升级依赖项时反应“TypeError:路径必须是字符串。收到未定义”

遍历对象数组-TypeError:无法读取未定义的属性“名称”

NodeJS - TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义

Nrwl:Storybook 给我错误 TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。收到未定义

Cloud Functions 错误:函数返回未定义、预期的 Promise 或值,以及 TypeError:无法读取未定义的属性“First_Name”