ReferenceError:未定义错误

时间:2015-12-23 18:41:40

标签: javascript node.js express

ReferenceError:代码中未定义错误:

app.post('/register', function(req, res) {
  var hash = bcrypt.hashSync(req.body.password, bcrypt.genSaltSync(10));
  var user = new User({
    firstName: req.body.firstName,
    lastName: req.body.lastName,
    email: req.body.email,
    password: hash,
  });
  user.save(function(err) {
    if (err) {
      var err = 'Something bad happened, try again!';
      if(err.code === 11000) {
        error = 'That email is already taken, try another.';
      }
      res.render('register.jade', { error: error });
    } else {
      res.redirect('/dashboard');
    }
  });
});

具体而言,错误就在这条线上我相信:

  res.render('register.jade', { error: error });

任何人都可以告诉我如何解决这个问题。当我输入时,我以为我定义了它:

  if(err.code === 11000) {
        error = 'That email is already taken, try another.';
   }

我收到的错误是:

/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:779
      catch(err) { process.nextTick(function() { throw err}); }
                                                       ^
ReferenceError: error is not defined
    at /Users/darceymckelvey/Documents/auth/app.js:57:44
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/lib/document.js:1747:19
    at handleError (/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:40:22)
    at next_ (/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:75:26)
    at fnWrapper (/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:186:18)
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/lib/model.js:290:15
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/lib/model.js:191:14
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/lib/model.js:135:9
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/lib/collection.js:484:20
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/lib/collection.js:597:14
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:469:9
    at resultHandler (/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:416:5)
    at /Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:778:13
    at Callbacks.emit (/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:95:3)
    at null.messageHandler (/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:249:23)
    at Socket.<anonymous> (/Users/darceymckelvey/Documents/auth/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:262:22)

1 个答案:

答案 0 :(得分:3)

抱歉 - 只需重新阅读您的问题/代码 - 这是一个范围问题:

user.save(function(err) {
    if (err) {
      var error = 'Something bad happened, try again!';
      if(err.code === 11000) {
        error = 'That email is already taken, try another.';
      }
      res.render('register.jade', { error: error });