2015-05-24 57 views
-4

我正在玩'Mean Machine'一書中的練習,創建一個API並驗證用戶並給出一個令牌。我在comparePassword()上得到'TypeError:undefined不是函數'。我究竟做錯了什麼?「TypeError:undefined不是函數」代表用戶的模式中的方法

這是我的錯誤。

server.js:69 
    var validPassword = user.comparePassword(req.body.password); 
          ^
TypeError: undefined is not a function 

這裏的,這似乎是造成問題的原因(按下一半)的代碼:

// more routes for our API will happen here 
apiRouter.post('/authenticate', function(req, res){ 
    // find the user 
    // select the name username explicitly 
    User.findOne({ 
     username: req.body.username 
    }).select('name username password').exec(function(err, user){ 

     if(err) throw err; 

     // no user with that username was found 
     if (!user){ 
      res.json({ 
       success: false, 
       message: 'Authentication failed. User not found' 
      }); 

     }else if (user) { 
      // check if password matches 
      var validPassword = user.comparePassword(req.body.password); 
      if(!validPassword){ 
       res.json({ 
        success: false, 
        message: 'Authentication failed. wrong password' 
       }); 
      } else { 

      // if user is found and password is right 
      // create a token 
      var token = jwt.sign({ 
       name: user.name, 
       username: user.username 
      }, superSecret, { 
       expiresInMinutes: 1440 // expires after 24 hours 
      }); 

      // return the information including token as JSON 
      res.json({ 
       success: true, 
       message: 'enjoy your token!', 
       token: token 
      }); 
     } 
     } 

    }); 
}); 

這裏的上述上下文中有server.js文件的其餘部分:

// Base Setup 
// ====================================== 

// CALL THE PACKAGES 
var express  = require('express'); // call express 
var app   = express(); // define our app using express 
var bodyParser = require('body-parser'); // get body-parser 
var morgan  = require('morgan'); // used to see requests 
var mongoose = require('mongoose'); // for working w/ our database 
var User  = require('./app/models/user'); 
var port  = process.env.PORT || 8080; // Set the port for our app 
var jwt = require('jsonwebtoken'); 

// super secret for creating tokens 
var superSecret = 'rockabyezebra'; 

//APP CONFIGURATION 
// Use body-parser so we can grab information from post requests 
app.use(bodyParser.urlencoded({extended: true })); 
app.use(bodyParser.json()); 

// configure our app to handle CORS requests 
app.use(function(req, res, next){ 
    res.setHeader('Access-Control-Allow-Origin', '*'); 
    res.setHeader('Access-Control-Allow-Methods', 'GET', 'POST'); 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, \ Authorization'); 
    next(); 
}); 


// log all requests to the console 
app.use(morgan('dev')); 

// connect to our database (hosted on mongolab.com) 
mongoose.connect('mongodb://blah:[email protected]:31852/app42'); 

// ROUTES FOR OUR API 
// ================================ 
var apiRouter = express.Router(); // get an instance of the express Router 


// basic route for the homepage 
app.get('/', function(req, res){ 
    res.send('Welcome to the home page'); 
}); 

// get an instance of the express router 
var apiRouter = express.Router(); 

// more routes for our API will happen here 
apiRouter.post('/authenticate', function(req, res){ 
    // find the user 
    // select the name username explicitly 
    User.findOne({ 
     username: req.body.username 
    }).select('name username password').exec(function(err, user){ 

     if(err) throw err; 

     // no user with that username was found 
     if (!user){ 
      res.json({ 
       success: false, 
       message: 'Authentication failed. User not found' 
      }); 

     }else if (user) { 
      // check if password matches 
      var validPassword = user.comparePassword(req.body.password); 
      if(!validPassword){ 
       res.json({ 
        success: false, 
        message: 'Authentication failed. wrong password' 
       }); 
      } else { 

      // if user is found and password is right 
      // create a token 
      var token = jwt.sign({ 
       name: user.name, 
       username: user.username 
      }, superSecret, { 
       expiresInMinutes: 1440 // expires after 24 hours 
      }); 

      // return the information including token as JSON 
      res.json({ 
       success: true, 
       message: 'enjoy your token!', 
       token: token 
      }); 
     } 
     } 

    }); 
}); 


// middleware to use for all requests 
apiRouter.use(function(req, res, next){ 
    // do logging 
    console.log('Somebody just visited our app'); 
    // we'll add more to the middleware in Chapter 10 
    // this is where we will authenticate users 
    next(); // make sure we go to the next routes and don't stop here 
}); 





// test route to make sure everything is working 
// accessed at GET http://localhost:8080/api 
apiRouter.get('/', function(req, res){ 
     res.json({ message: 'hooray! welcome to our api'}); 
}); 



// on routes that end in /users 
// --------------------------------------------------- 
apiRouter.route('/users') 

    // create a user (accessed at POST http://localhost:8080/users) 
    .post(function(req, res){ 

     // create a new instance of the user model 
     var user = new User(); 

     // set the users information (comes from the request) 
     user.name = req.body.name; 
     user.username = req.body.username; 
     user.password = req.body.password; 

     // save the user and check for errors 
     user.save(function (err) { 
      if (err){ 
      // duplicate entry 
      if (err.code ==11000) 
       return res.json({ success: false, message: 'A user with that username already exists. '}); 
      else 
       return res.send(err); 
      } 
      res.json ({ message: 'User created'}); 
     }); 
    }) 

    // get all the users (accessed at GET http://localhost:8080/api/users) 
    .get(function(req,res) { 
     User.find(function(err, users) { 
      if (err) return res.send(err); 

      // return the users 
      res.json(users); 
     }); 
    }); 

// on routes that end in /users/:user_id 
// ---------------------------------------------------- 
apiRouter.route('/users/:user_id') 

    // get the user with that id 
    .get(function(req, res) { 
     User.findById(req.params.user_id, function(err, user) { 
      if (err) return res.send(err); 

      // return that user 
      res.json(user); 
     }); 
    }) 

// update the user with this id 
    .put(function(req, res){ 
     User.findById(req.params.user_id, function(err, user) { 
      if (err) return res.send(err); 

      // set the new user information if it exists in the request 
      if(req.body.name) user.name = req.body.name; 
      if(req.body.username) user.username = req.body.username; 
      if(req.body.password) user.password = req.body.password; 

      // save the user 
      user.save(function(err){ 
       if (err) return res.send(err); 

      // return a message 
      res.json({ message: 'user updated'}); 
      }); 
     }); 
    }) 

    .delete(function(req, res){ 
     User.remove({ 
      _id: req.params.user_id 
      }, function(err, user) { 
       if (err) res.send(err); 

      res.json({ message: 'Successfully deleted'}); 
     }); 
    }); 




// REGISTER OUR ROUTES ---------------------------------- 
// all of our routes will be prefixed with /api 
app.use('/api', apiRouter); 

// START THE SERVER 
// ================================ 
app.listen(port); 
console.log('rockin\' on port ' + port + ' y\'all'); 

回答

3

我假設你正在爲你的數據使用貓鼬模塊。看起來您從未在您的UserSchema(其中User模型來自此處)定義方法comparePassword。您可以像這樣在模式中定義方法。 Google文檔here

var AnimalSchema = new Schema({ 
    name: String, 
    type: String 
}); 

AnimalSchema.methods.findSimilarType = function findSimilarType(cb) { 
    return this.model('Animal').find({ 
    type: this.type 
    }, cb); 
}; 
// Now when we have an instance of Animal we can call our // findSimilarType method and find all animals with a matching type. 

var Animal = mongoose.model('Animal', AnimalSchema); 
var dog = new Animal({ 
    name: 'Rover', 
    type: 'dog' 
}); 

dog.findSimilarType(function(err, dogs) { 
    if (err) return ... 
    dogs.forEach(..); 
}) 
+0

感謝mucho,非常有幫助,您是正確的。我在哪裏定義方法,我會拼錯某些東西:)現在它正在工作。誰知道爲什麼有人覺得需要'通過downvote飛'我原來的問題:( –

+0

@AgentZebra:你發佈了大量的代碼,並沒有產生一個簡單的測試用例。將來,一個簡單的測試用例是第一件事你應該試着解決你的問題,如果你仍然無法找到它,那麼用測試用例創建一個stackoverflow問題。http://stackoverflow.com/help/mcve –

相關問題