【问题标题】:Is there a way to set up mongoose to check if two values match in a single schema?有没有办法设置猫鼬来检查两个值是否在一个模式中匹配?
【发布时间】:2021-10-16 11:53:21
【问题描述】:

我是 node.js 的新手,需要进行一些基本验证以检查两个字段(在本例中为密码)是否匹配,如果不匹配,则显示错误消息。我现在不担心加密,因为它超出了范围,因为这是 uni 任务的最后一部分。

我尝试创建可以传递给另一个变量的变量,但这并没有太大帮助。

这是我所拥有的缩短的 sn-p。

架构:

const customerSchema = new mongoose.Schema(
    {
        password: 
        {
            type: String, 
            required: true,
        },

        rePassword:
        {
            type: String, 
            required: true,
        },

已发布

app.post("/", function(req,res)
{
    let newCustomer = Customer(
        {
            password: req.body.cPass,
            rePassword: req.body.cConfPass,

任何帮助表示赞赏:)

【问题讨论】:

    标签: node.js mongodb mongoose schema mongoose-schema


    【解决方案1】:

    解决了!

    事实证明我需要在稍后运行该函数之前执行它,它看起来像

        if(req.body.cPass != req.body.cConfPass)
        {
            console.log("passwords don't match")
        }
        else
        {
            newCustomer.save();
        }
    

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 2023-03-19
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2011-05-12
      • 2011-04-08
      • 2021-08-10
      • 1970-01-01
      相关资源
      最近更新 更多