【发布时间】:2014-12-27 09:52:11
【问题描述】:
有一些问题!
错误是
local:
{ password: '$2a$08$kSflSzcciqWN78nfqAu/4.ZBZaXkqb19bEypeWcuSxg89yPNuijYO',
email: '***@gmail.com' } } has no method 'usersCharacters'
但它确实有方法! 我不确定它是否被正确导出。据我所知,我的做法与用户使用的其他方法类似,但在这里似乎不起作用。
user.js 模型文件
....
module.exports = mongoose.model('User', UserSchema);
var User = mongoose.model('User', UserSchema);
/* method works :D ! -- not sure if over complicated though! */
UserSchema.methods.usersCharacters = function(email,cb){
User.findOne( {'local.email' : email }).exec(function(err, user){
if (err) console.log("shit");
var _return = [];
user.characters.forEach(function(err, i){
Character.findOne({ "_id" :user.characters[i] }).exec(function(err2, dude){
_return.push(dude);
/* Can't think of a smarter way to return :( */
if ( i == user.characters.length-1)
cb(_return);
});
});
});
};
routes.js
/* This doesn't work! I am wondering how I might be able to return this users characters --
*
* The error is here btw! TypeError: Cannot call method 'usersCharacters' of undefined -- line with character : ****
*/
app.get('/profile', isLoggedIn, function(req, res) {
var mongoose = require('mongoose');
var UserSchema = mongoose.model('User', UserSchema);
console.log(UserSchema);
// ERROR ON THIS LINE! : (
characters : req.user.usersCharacters(req.user.email, function(_characters){
console.log("list of characters: " + _characters);
return _characters;
res.render('profile.ejs', {
user : req.user, // get the user out of session and pass to template
characters : characters
});
这里有更多我的模型文件代码的要点:
【问题讨论】: