How do you reference a Mongoose model in another file in a MEAN application? I am in login.js
and need to reference user.js
to findOrCreate
a user. Are my paths wrong or did I initialize the model incorrectly? My error is undefined is not a function on the line to create a user.
File structure
root
models
user.js
passport
login.js
...
...
login.js
var User = require('./../models/user');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
module.exports = function(passport){
// Google login
passport.use(new GoogleStrategy({
clientID: 'id',
clientSecret: 'secret',
callbackURL: 'http://127.0.0.1:3000/auth/google_oauth2/callback'
}, function(accessToken, refreshToken, profile, done) {
// Problem here?
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}));
};
user.js
var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
googleId: {type: String, unique: true, required: true}
});
Change your user.js file:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = mongoose.model(
'User',
new Schema({
googleId: {type: String, unique: true, required: true}
}
));
You might know more about Schema class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.