在我的nodejs应用中,在用户猫鼬模式内部,我有以下方法:

/*
 * Passport-Local Mongoose will add a username, hash and salt field to store the username, the hashed password and the salt value.
 */
var User = new Schema({
  email: String,
  token: String,
  twitter: {
    id: String,
    token: String,
    displayName: String,
  }
});

    /*
 * Find user by twitter id.
 */
User.statics.findOrCreateByTwitterId = function (token, tokenSecret, profile, fn) {
  this.findOne({
    'twitter.id': profile.id
  }, function (err, user) {
    if (err) return fn(err, null);
    if (user) {
      return fn(null, user)
    };

    // create user
    var newUser = new User();
    newUser.username = profile.username;
    newUser.twitter.id = profile.id;
    newUser.token = token;
    newUser.displayName = profile.displayName;

    // create user
    newUser.save(function (err) {
      if (err) {
        return fn(null, null);
      }
      return fn(null, newUser)

    });

  });
};

User.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', User);


当它叫我收到:

2015-02-24T07:47:24.162Z - debug: Mongoose:  -  users  -  findOne  -  { 'twitter.id': '90411931' }
2015-02-24T07:47:24.327Z - error: Caught exception:  TypeError: object is not a function


第32行是:

var newUser = new User();


看到什么问题了吗?

最佳答案

好吧...猫鼬。架构无法实例化。您需要从这样的模式创建模型,

var UserModel = mongoose.model( 'UserModel', User );

关于javascript - 为什么收到“对象不是函数”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28690586/

10-14 19:04
查看更多