为什么我收到'对象不是函数;
Why I receive 'object is not a function'?
在我的nodejs应用程序中,在User mongoose模式中,我有这样的方法:
/*
* 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 );
要从模式中引用当前模型,可以执行var newUser = new this.constructor();
要创建新用户,需要new
模型,而不是User
模式。因此,在模型的静态函数中保存初始this
,这样它就不会在findOne
回调中丢失一次,然后使用它来创建用户:
User.statics.findOrCreateByTwitterId = function (token, tokenSecret, profile, fn) {
var model = this;
model.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 model();
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)
});
});
};
相关文章:
- 创建对象函数原型和代码是错误的
- JavaScript模块模式-如何在使用对象/函数之前激发构造函数/init函数
- 如何从onclick字符串中引用javascript对象函数
- 更改对象函数仅用于示例
- javascript和jQuery的嵌套对象函数中的变量范围
- 将对象函数传递给请求动画帧时丢失对象引用
- 对象函数返回函数而不是值
- 使用onclick调用属性对象函数
- javascript拉斐尔对象函数传递
- 如何从onClick事件调用对象函数
- 可以't获取具有“t”的对象变量;这个“;由setTimeout()函数调用的对象函数中的属性
- 对象没有't继承父对象函数
- 调用父对象函数
- "这个“;对象函数内部的引用
- 将变量添加到对象函数调用的末尾
- 传单错误:对象函数没有方法'createIcon'在LayerGroup中创建自定义图标标记时
- TypeError:对象函数Object(){〔本机代码〕}没有方法'方法'
- 挖空 - 单击绑定到对象函数 - 范围问题
- 类型错误: 对象函数...没有方法“打开”
- JavaScript 对象函数