Node.js 如何使用未定义的变量Node js解决此问题?

Node.js 如何使用未定义的变量Node js解决此问题?,node.js,express,momentjs,Node.js,Express,Momentjs,我们正在尝试将这3个生日、生日和生日变量转换为年龄。我们从前端获取这3个值,并希望在node js后端将它们转换为age并存储在用户数据库中。我们使用矩js将其转换为年龄 module.exports = { async CreateUser(req, res) { const schema = Joi.object().keys({ username: Joi.string() .required(), email: Joi.string()

我们正在尝试将这3个生日、生日和生日变量转换为年龄。我们从前端获取这3个值,并希望在node js后端将它们转换为age并存储在用户数据库中。我们使用矩js将其转换为年龄

module.exports = {
  async CreateUser(req, res) {
    const schema = Joi.object().keys({
      username: Joi.string()
        .required(),
      email: Joi.string()
        .email()
        .required(),
      password: Joi.string()
        .required(),
        birthday: Joi.number().integer()
        .required().min(2).max(2),
        birthmonth: Joi.number().integer()
        .required().min(2).max(2),
        birthyear: Joi.number().integer()
        .required(),
        age:age 
    });

    const { error, value } = Joi.validate(req.body, schema);
    if (error && error.details) {
      return res.status(HttpStatus.BAD_REQUEST).json({ msg: error.details })
    }

    const userEmail = await User.findOne({
      email: Helpers.lowerCase(req.body.email)
    });
    if (userEmail) {
      return res
        .status(HttpStatus.CONFLICT)
        .json({ message: 'Email already exist' });
    }

    const userName = await User.findOne({
      username: Helpers.firstUpper(req.body.username)
    });
    if (userName) {
      return res
        .status(HttpStatus.CONFLICT)
        .json({ message: 'Username already exist' });
    }

    return bcrypt.hash(value.password, 10, (err, hash) => {
      if (err) {
        return res
          .status(HttpStatus.BAD_REQUEST)
          .json({ message: 'Error hashing password' });
      }
     const age = moment().diff(moment([birthyear, birthmonth - 1, birthday]), 'years');

      const body = {
        username: Helpers.firstUpper(value.username),
        email: Helpers.lowerCase(value.email),
        birthday: (value.bday),
         birthmonth: (value.month),
       birthyear: (value.month),
        password: hash,
       age:age
      };
      User.create(body)
        .then(user => {
          const token = jwt.sign({ data: user }, dbConfig.secret, {
            expiresIn: '5h'
          });
          res.cookie('auth', token);
          res
            .status(HttpStatus.CREATED)
            .json({ message: 'User created successfully', user, token });
        })
        .catch(err => {
          res
            .status(HttpStatus.INTERNAL_SERVER_ERROR)
            .json({ message: 'Error occured' });
        });
    });
  },
当我在command prompth前端提交注册按钮时出现错误,它显示未定义生日,并在此行下显示:

const age = moment().diff(moment([birthyear, birthmonth - 1, birthday]),
我把它放在身体上方,插入了年龄:身体内部的年龄


我确信其他2个值也会发生同样的错误。怎么了?我们如何解决此问题?

您从未在提供的代码中的任何位置定义变量
生日
生日
生日
,因此错误是有意义的

我不熟悉您正在使用的
Joi
对象验证库,但将其视为
value。password
包含上述bcrypt调用中的密码,我认为您可以在
value
上作为属性访问这些字段。试试这个:

 const age = moment().diff(moment([value.birthyear, value.birthmonth - 1, value.birthday]), 'years');

您可能会在
const body={…}
代码块中遇到类似的错误,因为您使用的属性名称与架构定义中的不同,所以请仔细检查所有
value.*
行以修复这些错误。

感谢您的回复。就身体对象而言,年龄:年龄是正确的(身体对象中的最后一个)?@NewTechLover您可能必须将其更改为
age:age.toDate()
,因为它目前是数据库可能无法理解的
时刻
日期对象
toDate
将它转换为一个正常的js日期对象,该对象应该可以工作(尽管这取决于您使用的数据库和数据库库)mongoose@NewTechLoverMongoose将与moment的
.toDate()配合使用