gpt4 book ai didi

javascript - 为什么我收到 'object is not a function' ?

转载 作者:行者123 更新时间:2023-12-02 16:25:57 26 4
gpt4 key购买 nike

在我的nodejs应用程序中,在User mongoose schema中我有这个方法:

/*
* 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();

你发现什么问题了吗?

最佳答案

嗯... mongoose.Schema 无法实例化。您需要从这样的模式创建一个模型,

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

关于javascript - 为什么我收到 'object is not a function' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28690586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com