ReferenceError:未定义nome

时间:2019-06-28 05:05:36

标签: node.js express

我正在尝试使函数检索回调。首先,错误是回调参数不是一个函数,然后我试图修正其语法,即未定义参数。

控制器:(第10行发生错误):

//Tried to declare like "function registraU(nome, ... ())
    const registraUsuario = (nome, email, password, (e, usuarioCriado) => {
        UsuarioModel.findOne({ email: email }, (e, match) => {
          if (e) { return callback(e); }

          if (match !== null) {  
              return callback(null, null);
            } else {
                var hash = bcrypt.hashSync(password, 10);
                password = hash;
                novoUsuario = {
                    nome: nome,
                    email: email,
                    password: password
                }

                var temp = new UsuarioModel(novoUsuario);

                temp.save(function(e, usuarioCriado){
                    if(e){console.log(e)};

                    return callback(null, usuarioCriado);
                });
            }
        });
    });

这是调用它的代码:

passport.use('local-registro', new LocalStrategy({
     nomeField: 'nome',
     emailField: 'email',
     passwordField: 'password',
     passReqToCallback : true 
   },
     (req, nome, email, password, done) => {

      UsuarioController.registraUsuario(nome, email, password, (e, callback) => {
        if(e) {return done(e); }

        if(!novoUsuario){
          return done(null, false, req.flash({"erroRegistro": "Email já cadastrado"}));
        } else {
          return done(null, novoUsuario);
        }
      });
     }
  ));

enter image description here

1 个答案:

答案 0 :(得分:1)

您声明是一种es6箭头函数,但您忘记添加函数主体...

const registraUsuario = (nome, email, password, (e, usuarioCriado) => {...});

但是其余的人在哪里呢?

const registraUsuario = (nome, email, password, (e, usuarioCriado) => {...}) => {
   // your function body
}

这就是您的代码损坏的原因...它不解释为函数声明。