I have a strange behavior in my Express app which I cannot understand. I have a user model file (user.js) which contains some schemas and some functions. When I require the file from my routes file (users.js) I can access Schemas but cannot call functions as they return 'myFunc is not a function'. This happens only in case I have multiple schemas, in case of one schema everything is perfect. Could you please help me to figure out?
This one returns: userModels.User.addUser is not a function
user.js
...
const UserModels = module.exports = {
'User': mongoose.model('User', UserSchema),
'Token': mongoose.model('Token', TokenSchema)
}
module.exports.addUser = function(newUser, callback){
console.log('test');
}
...
users.js
...
userModels.User.addUser(newUser, function(err, user){
if(err){
res.json({success: false, msg: "Failed to register."});
} else{
res.json({success: true, msg: 'User has been registered'});
}
});
...
This one passes:
...
const User = module.exports = mongoose.model('User', UserSchema);
module.exports.addUser = function(newUser, callback){
console.log('test');
}
...
User.addUser(newUser, function(err, user){
if(err){
res.json({success: false, msg: "Failed to register."});
} else{
res.json({success: true, msg: 'User has been registered'});
}
});
...
via Emil Gurbanov
No comments:
Post a Comment