Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何验证输入内容中的错误字符并取消其影响?(MySQL请求/Joi)_Mysql_Reactjs_Express_Joi - Fatal编程技术网

如何验证输入内容中的错误字符并取消其影响?(MySQL请求/Joi)

如何验证输入内容中的错误字符并取消其影响?(MySQL请求/Joi),mysql,reactjs,express,joi,Mysql,Reactjs,Express,Joi,我创建了一个表单,用户可以在其中输入他们的信息。 我使用Joi验证表单的内容,使用MySQL与数据库通信 不幸的是,如果我的用户在输入中输入一个“”,我就有一个来自MYSQL的语法错误。 我怎样才能自动更改它? 我试着用\'来代替,但我不知道这是否是个好方法。。。我觉得不是 我的验证模型不检查危险字符,如“或\或”。我希望将它们保存在我的数据库中,但要安全。我希望我的用户可以输入“It's…”(如果他愿意) 这是我的验证模型: function validateUser(user){ c

我创建了一个表单,用户可以在其中输入他们的信息。 我使用Joi验证表单的内容,使用MySQL与数据库通信

不幸的是,如果我的用户在输入中输入一个“”,我就有一个来自MYSQL的语法错误。 我怎样才能自动更改它? 我试着用\'来代替,但我不知道这是否是个好方法。。。我觉得不是

我的验证模型不检查危险字符,如“或\或”。我希望将它们保存在我的数据库中,但要安全。我希望我的用户可以输入“It's…”(如果他愿意)

这是我的验证模型:

function validateUser(user){
    const schema = {
        firstname: Joi.string().required(),
        lastname: Joi.string().required(),
        phone: Joi.string().required(),
        bestcontact: Joi.string().min(5).max(5).required(),
        mail: Joi.string().optional().allow(null).allow(""),
        address: Joi.string().allow(null).optional().allow(""),
        suburb: Joi.string().allow(null).optional().allow(""),
        state: Joi.string().allow(null).optional().allow(""),
        postalcode: Joi.string().allow(null).optional().allow(""),
        birthday: Joi.date().allow("").allow(null),
        iduser: Joi.number().optional()
    };
    return Joi.validate(user, schema);
}
这是我的SQL请求:

const queryString=`INSERT-INTO-user(\`iduser\`,\`firstname\`,\`lastname\`,\`phone\`,\`mail\`,\`substrator\`,\`state\`,\`postalcode\`,\`birth\`,\`bestcontact\`)值(${user.iduser},'${user.firstname},'${user.lastname},'${user.phone},'${user.mail},user.mail},user.state},'$},'${${user.postalcode}'、'${user.birth}'、'${user.bestcontact}')`

提前感谢

如果验证失败,Joi会出错,或者您可以这样检查错误

const result = schema.validate(body);   
    if (result.error) {
        throw result.error;
    }   
    return schema.validate(body);
}
函数调用部分

try {
    await validateUser(user);
    //success to validate
} catch(error) {
    //validate error(failed to validate)
}

如果验证失败,Joi会出错,或者您可以像这样检查错误

const result = schema.validate(body);   
    if (result.error) {
        throw result.error;
    }   
    return schema.validate(body);
}
函数调用部分

try {
    await validateUser(user);
    //success to validate
} catch(error) {
    //validate error(failed to validate)
}

当我输入“test”test时,Joi不返回错误:/(但我的内容中有一个)这是我的验证
const{error}=validate(req.body);if(error)返回res.status(400)。send(error.details[0]。message)
我认为在更改版本时,返回值已更改。然后,您可以在result Objects中使用error属性进行验证。当我输入“test”test时,Joi不会返回错误:/(但我的内容中有一个“),这是我的验证
const{error}=validate(req.body);if(error)返回res.status(400)。send(error.details[0]。message);
我认为在更改版本时,返回值已更改。然后您可以在结果对象中使用error属性进行验证